Richie378 Richie378 -4 years ago 72
MySQL Question

Warning: mysqli_query(): Couldn't fetch mysqli

I am new to PHP and not familiar with many of its rules, so this could possibly be a stupid question.

I have a database with top level categories and subcategories combined into one table. I want to first print out all the top-level categories, and then printout the subcategories associated with that category.

Here is my code:

<?php
session_start();

include_once "/mysqli_connect.php";

$categories0 = mysqli_query($conn, "SELECT * FROM categories WHERE type = 'category'");


mysqli_close($conn);
?>

<html>
<head>
<meta charset="UTF-8" />
</head>
<body>

<div id="wrap" class="animate">

<?php

while ($categories = mysqli_fetch_array($categories0, MYSQLI_ASSOC)) {

$catecory_name = $categories['category'];
echo '
<div class="content">
<div class="content_container no_padding">
<div class="content_container header">
<p>'.$categories['category'].'</p>
</div>
';

$subcategories0 = mysqli_query($conn, "SELECT * FROM categories WHERE type = 'subcategory'");

while ($subcategories = mysqli_fetch_array($subcategories0, MYSQLI_ASSOC)) {
echo $subcategories['category'];
//mysqli_free_result($subcategories0);
}

echo '
</div>
</div>
';

}

?>

</div>
</div>


</body>
</html>


Here is the connection script:

<?php

DEFINE ('DB_USER', '*');
DEFINE ('DB_PASSWD', '*');
DEFINE ('DB_HOST', '*');
DEFINE ('DB_NAME', '*');

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);

if(!$conn){
die('Database connection error');
}

echo '<!-- Connected to database -->'

?>


It returns the following error:

Warning: mysqli_query(): Couldn't fetch mysqli


When both queries are above the doctype everything is fine, but when the second query is below the doctype the error occurs.

The first query always runs without problems, it is the second one that returns the error.

I can't seem to figure out what is going on, if anyone can help me that would be appreciated.

Answer Source

You forget to close your while loop. check comment in line where you need to close it.

<?php
$categories0 = mysqli_query($conn, "SELECT * FROM categories WHERE type = 'category'");
?>
<!DOCTYPE html>

<?php
while ($categories = mysqli_fetch_array($categories0, MYSQLI_ASSOC)) {// need to close your loop

$catecory_name = $categories['category'];
echo '
<div class="content">
<div class="content_container header">
<p>'.$categories['category'].'</p>
</div>
';
}// close here
$subcategories0 = mysqli_query($conn, "SELECT * FROM categories WHERE type = 'subcategory'");
// The above line is where the error occurs 

while ($subcategories = mysqli_fetch_array($subcategories0, MYSQLI_ASSOC)) {
    echo $subcategories['category'];

}


?>

UPDATED

Remove close connection from top because after it your query will not execute. Your connection variable is vanished after your connection is closed.

<?php
session_start();

include_once "/mysqli_connect.php";

$categories0 = mysqli_query($conn, "SELECT * FROM categories WHERE type = 'category'");


?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download