我收到这条消息,为什么? mysqli_query():无法获取mysqli

时间:2018-05-25 23:24:34

标签: php html mysql sql

这是连接页面,它适用于我的索引文件。现在的问题是:我需要在另一个页面上运行另一个sql,它应该是这样的:SELECT * FROM products WHERE id = $var。但它给了我“mysqli_query():无法获取mysqli”当我尝试这样做时,我该怎么办?

connection.php

$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$db = "movies";

//connecting to the database
$conn = mysqli_connect("$host","$dbusername","$dbpassword","$db");
mysqli_set_charset($conn,'utf8');

//check for error in connection
if (!$conn) {
  die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}

//Selecting table and executing query
$sql = 'SELECT * FROM products';
$query = mysqli_query($conn, $sql);

if (!$query) {
  die ('SQL Error: ' . mysqli_error($conn));
}

mysqli_close($conn);

- 另一页 -

<?php

include "connection.php";

$movieid = $_GET['movieid'];

$sql = 'SELECT * FROM products WHERE id = "$movieid"';
$query = mysqli_query($conn, $sql);

$movie_title = $row['title'];
echo "$movie_title";
?>

1 个答案:

答案 0 :(得分:0)

出现此错误的原因是您正在关闭connenction.php文件上的连接。您只需删除此行:

mysqli_close($conn);