Warning: mysqli_close(): Couldn't fetch mysqli
It's difficult to tell with the indentation, but in some cases you will close the connection twice...
$conn->close();
}
mysqli_close($conn);
These will both close the connection, so the second one will fail.
Probably easier to remove the $conn->close();
as it will always fall through to the second close.
mysqli_query(): Couldn't fetch mysqli error
I suspect you're calling the function multiple times. But it calls mysqli_close($conn)
at the end, so when you try to use it the next time you get an error, because $conn
can't be used any more.
Get rid of that line, and close the connection when the script is all done using MySQL (or don't bother, it will be closed automatically when the script ends).
Warning: mysqli_query(): Couldn't fetch mysqli
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'");
?>
mysqli::query(): Couldn't fetch mysqli
Probably somewhere you have DBconnection->close();
and then some queries try to execute .
Hint: It's sometimes mistake to insert ...->close();
in __destruct()
(because __destruct
is event, after which there will be a need for execution of queries)
Why Warning: mysqli_query(): Couldn't fetch mysqli?
The problem is that the connection is closed when you call getData()
after you have created the Display
object.
The getData()
call in your constructor doesn't make any sense, because you don't use/save the return value. So when then constructor is executed you open a connection, send a select query (which return value you don't save) and then close the connection. After that a getData()
call results in your error message.
You can either save the result of your getData()
call from the constructor in a private field and access it later or remove the getData()
and $this->close();
call from the constructor and call them from outside.
Warning: mysqli_query(): Couldn't fetch mysqli in file on line number
Barmar is correct that $this->dbc is not a valid mysqli object. You are using the require_once with the file like a function. Make the config.php a class, and make the db connection from there. See here for an example of what I'm referring to. https://gist.github.com/jonashansen229/4534794
PHP Warning: mysqli::query(): Couldn't fetch mysqli
Your connection has been closed, after execution of first SELECT Statement, it means connection closed to early:
$conn->close();
You need to use close()
after your all queries or re build connection. ist one is the better option.
You are getting user input $_REQUEST['key']
, it means your query is open for SQL injection, this will help you to understand how can you prevent your code with SQL injection: How can I prevent SQL injection in PHP?
Related Topics
Output Text File with Line Breaks in PHP
How to Set the Cookie Content with Curl
Laravel 5.5 Error Base Table or View Already Exists: 1050 Table 'Users' Already Exists
Transfer Variables Between PHP Pages
While ($Row = MySQL_Fetch_Array($Result)) - How Many Loops Are Being Performed
Disable Browser Cache in PHP or JavaScript in a Flash Application
Getting a PHP Pdo Connection from a MySQL_Connect()
Warning: Array_Combine(): Both Parameters Should Have an Equal Number of Elements
Php: Check for Duplicate Values in a Multidimensional Array
3Rd Party Dependency Conflict in Developing Wordpress Plugin
How to Prevent from Printscreen of My Webpage
How to Paginate Lines in a Foreach Loop with PHP
PHP Curly Braces in Array Notation
PHP - Merge Two Arrays (Same-Length) into One Associative
Differencebetween PHP Require and Include
PHP Datetime Round Up to Nearest 10 Minutes