MySQLi::Query(): Couldn't Fetch MySQLi

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)

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_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.

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?

MySQLi Query error Couldn't fetch mysqli

The solution:

I changed line 4 from:

$mysqli = NEW MySQLi("localhost","root","","dbproject");

to

$mysqli = NEW MySQLi("localhost:3307","root","","dbproject");

Adding the port somehow fixed it.

Warning: mysqli::query(): Couldn't fetch mysqli - PHP7.1 & MySQL5.7

I felt that I needed to step in here, given the answers which IMHO did not answer the question in question.

As I stated in comments, the quotes need to be removed from the database declaration.

$connect = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

Since those are constants.

References:

  • http://php.net/manual/en/function.define.php
  • http://php.net/manual/en/function.constant.php

Error reporting would also have been of help:

  • http://php.net/manual/en/function.error-reporting.php

Edit:

As user3783243 pointed out in comments:

Additionally when you get the connection working $message will be a result object, you will need to fetch that. – user3783243

is right. Echoing the query is simply the result set. You need to loop over (successful) results and there are many ways to do this.

You appear to be new to using a database. If you look through the manual inside the mysqli_query() function on PHP.net http://php.net/manual/en/mysqli.query.php, you will find many examples to fetch and show the results from the query, given that it was successful.

If you don't see results, then it (the query) may have failed. This is when you need to check for errors using mysqli_error($connect) on the query.

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'");


?>


Related Topics



Leave a reply



Submit