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

Den*_*nis 15 php mysql mysqli

我有一个问题,我无法从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());
}
Run Code Online (Sandbox Code Playgroud)

所以问题如下:

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

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

require 'dbfunctions.php';
Run Code Online (Sandbox Code Playgroud)

我的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;
}
Run Code Online (Sandbox Code Playgroud)

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

if ($userid == -1)
    {
        showNotAuthorizedPage();
    } else {
        myAccountPage();
    }
Run Code Online (Sandbox Code Playgroud)

并且myAccountPage()函数在与getUserPicPath()函数相同的文件中声明,此getUserPicPath()函数调用如下:

<div id="tabs-2">
    <p><?php getUserPicPath(); ?></p>
  </div>
Run Code Online (Sandbox Code Playgroud)

我在我的网页上使用了标签(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
Run Code Online (Sandbox Code Playgroud)

hof*_*n41 13

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

unset($connection);
Run Code Online (Sandbox Code Playgroud)

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