This question already has an answer here:
- Warning: mysqli_query(): Couldn't fetch mysqli 2 answers
- mysqli::query(): Couldn't fetch mysqli 4 answers
This is the connection page, and it works well with my index file. Now the question is: I need another sql to be run on another page where it is supposed to go like this: SELECT * FROM products WHERE id = $var
. But it gives me "mysqli_query(): Couldn't fetch mysqli" when I try to do it, what should I do?
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);
-- another page--
<?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";
?>
</div>