【问题标题】:Warning: mysqli_query(): Couldn't fetch mysqli [duplicate]警告:mysqli_query():无法获取 mysqli [重复]
【发布时间】:2014-09-18 08:19:51
【问题描述】:

我有一个问题,我无法从我的 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());
}

所以问题如下:

我有一个包含以下代码的 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());
}

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

require 'dbfunctions.php';

我的 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;
}

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

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

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

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

我在我的网页上使用标签 (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

【问题讨论】:

  • 这不是可用的完整信息。就像您检查连接错误一样,您也应该检查查询错误。我对 mysqli 不熟悉,但是如果您打开手册,您会发现名称上带有 error 的内容。
  • 您为什么要在getUserPicPath() 中运行您的查询两次?第一次通话后的echo 会给你什么?
  • 我还建议您查看mysqli_error() 中的内容,这样您就可以看到数据库传回的错误是什么
  • 我似乎没有得到任何错误,一切都是空的,除了显示“警告:mysqli_query():无法获取mysqli”之外什么都没有。但是我确实注意到,当我在 myaccount.php 中直接调用该函数时,它按预期工作,因此使用 而不是使用 似乎有效(但这不是我想要的……)。我已经编辑了我的问题并在 php/mysqli 返回的错误中添加了更多详细信息
  • 我认为您的数据库连接工作流程有问题。无论如何,您为什么不删除 mysqli_close(db_connect()) 语句并尝试在没有它的情况下运行脚本。无论哪种方式,当脚本结束时,mysql 连接通常会自动关闭

标签: php mysql mysqli


【解决方案1】:

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

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

    // enter code here
}

【讨论】:

    【解决方案2】:

    我认为是因为当你第一次关闭数据库连接时,你忘了做:

    unset($connection);
    

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

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2019-03-25
      • 2018-09-14
      • 2023-03-29
      • 2015-01-23
      • 2014-08-14
      相关资源
      最近更新 更多