php 警告:mysqli_query():无法获取 mysqli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31218188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:18:32  来源:igfitidea点击:

Warning: mysqli_query(): Couldn't fetch mysqli

phpmysqlmysqli

提问by Richie378

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

我是 PHP 的新手,不熟悉它的许多规则,所以这可能是一个愚蠢的问题。

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.

当两个查询都在 doctype 之上时,一切都很好,但是当第二个查询低于 doctype 时,就会发生错误。

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.

我似乎无法弄清楚发生了什么,如果有人可以帮助我,将不胜感激。

回答by Saty

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

您忘记关闭 while 循环。在需要关闭它的地方检查注释。

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


?>

回答by Richie378

Today I have phased the same problem. But There is little mistake that you did in code.

今天我已经分阶段解决了同样的问题。但是你在代码中犯的错误很少。

You are trigger select statement this:

您正在触发选择语句:

See. You are now closing the connection with mysqli_close($conn);

看。您现在正在关闭与 mysqli_close($conn); 的连接;

and then in while loop you are fetching data. If connection has been closed then how can php take data from mysql table?

然后在while循环中您正在获取数据。如果连接已关闭,那么 php 如何从 mysql 表中获取数据?

Just remove mysqli_close($conn); statement and run.

只需删除 mysqli_close($conn); 声明并运行。

In the last line you can put this code. After all the operation.

在最后一行中,您可以放置​​此代码。全部操作后。