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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24973330/
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 17:34:41  来源:igfitidea点击:

Warning: mysqli_query(): Couldn't fetch mysqli

phpmysqlmysqli

提问by Dennis

I have a problem where I can not retrieve the result from my MySQL database (via PHP). I use the same function in other places and it works flawlessly. However at this point i keep getting the "Warning: mysqli_query(): Couldn't fetch mysqli" error. Details of the problem are explained below. I use a quite similar function elsewhere (getAllCountries as seen below) in my PHP which does work perfectly:

我遇到了无法从 MySQL 数据库(通过 PHP)检索结果的问题。我在其他地方使用相同的功能,它完美无缺。但是此时我不断收到“警告:mysqli_query():无法获取mysqli”错误。问题的详细信息如下所述。我在我的 PHP 中的其他地方使用了一个非常相似的函数(如下所示的 getAllCountries),它运行良好:

function getAllCountries()
{
    $result = db_query("SELECT countryid, name FROM country ORDER BY name ASC");

    echo "<select class=addresscountry name=country>";
    while($row = mysqli_fetch_array($result)) {
      echo '<option value="' . $row['countryid'] . '">' . $row['name'] . '</option>';
    }
    echo "</select>";

    mysqli_close(db_connect());
}

So the problem is the following:

所以问题如下:

I have a php file containing the following code:

我有一个包含以下代码的 php 文件:

<?php
require 'includes/functions.php';

function getUserPicPath()
{
    $userid = $_SESSION['userid'];

    $result = db_query("SELECT picture FROM user WHERE userid='$userid'");

    while($row = mysqli_fetch_array($result)) {
        $picturepath = $row['picture'];
    }

    echo $picturepath;

    mysqli_close(db_connect());
}

my functions.php file has the following line (together with other non-relevant functions):

我的functions.php文件有以下几行(连同其他不相关的函数):

require 'dbfunctions.php';

and my dbfunctions.php looks like this:

我的 dbfunctions.php 看起来像这样:

<?php
function db_connect()
{
    require ".db_password.php";

    static $connection;

    if(!isset($connection)) {
        $connection = mysqli_connect('localhost',$username,$password,$dbname);
    }

    if($connection === false) {
        return mysqli_connect_error(); 
    }

    return $connection;
}

function db_query($query) 
{
    $connection = db_connect();

    $result = mysqli_query($connection,$query);

    return $result;
}

In my PHP document I call the following function :

在我的 PHP 文档中,我调用了以下函数:

if ($userid == -1)
    {
        showNotAuthorizedPage();
    } else {
        myAccountPage();
    }

and the myAccountPage() function is declared in the same file as the getUserPicPath() function, this getUserPicPath() function is called as follows:

并且 myAccountPage() 函数与 getUserPicPath() 函数在同一个文件中声明,这个 getUserPicPath() 函数调用如下:

<div id="tabs-2">
    <p><?php getUserPicPath(); ?></p>
  </div>

I use the tabs (http://jqueryui.com/tabs/#default) on my webpage and that is where i want to call it in. The myAccountPage() function which gives the following error :

我在我的网页上使用选项卡(http://jqueryui.com/tabs/#default),这就是我想调用它的地方。 myAccountPage() 函数给出了以下错误:

Warning: mysqli_query(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\xxx\zzz\www\Project Files\includes\dbfunctions.php on line 29
Call Stack
#   Time    Memory  Function    Location
1   0.0000  256880  {main}( )   ..\myaccount.php:0
2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
4   0.0070  285528  db_query( ) ..\myaccount.php:11
5   0.0070  285624  mysqli_query ( )    ..\dbfunctions.php:29

( ! ) Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 13
Call Stack
#   Time    Memory  Function    Location
1   0.0000  256880  {main}( )   ..\myaccount.php:0
2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
4   0.0080  285768  mysqli_fetch_array ( )  ..\myaccount.php:13

( ! ) Notice: Undefined variable: picturepath in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 17
Call Stack
#   Time    Memory  Function    Location
1   0.0000  256880  {main}( )   ..\myaccount.php:0
2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121

( ! ) Warning: mysqli_close(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0000  256880  {main}( )   ..\myaccount.php:0
2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
4   0.0100  285864  mysqli_close ( )    ..\myaccount.php:19

回答by hofan41

I think it is because when you close the database connection the first time, you forget to do:

我想是因为第一次关闭数据库连接的时候忘记了:

unset($connection);

And then when you try connecting to the database again, it craps out because it is still set to the closed connection.

然后当您再次尝试连接到数据库时,它会失败,因为它仍然设置为关闭的连接。

回答by Vikas Jangra

You forgot the include your database connection. Just add the $connectionto your sql query:

您忘记了包含您的数据库连接。只需添加$connection到您的 sql 查询:

function getAllCountries()
{
    $result = db_query($connection,"SELECT countryid, name FROM country ORDER BY name ASC");

    // enter code here
}