警告:mysqli_query():无法获取mysqli [英] Warning: mysqli_query(): Couldn't fetch mysqli

查看:324
本文介绍了警告:mysqli_query():无法获取mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法从MySQL数据库(通过PHP)检索结果.我在其他地方使用了相同的功能,并且可以正常工作.但是在这一点上,我不断收到警告:mysqli_query():无法获取mysqli"错误.问题的详细内容在下面说明. 我在PHP的其他地方(如下所示的getAllCountries)使用了一个非常类似的函数,该函数运行良好:

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:

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文件:

I have a php file containing the following code:

<?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文件具有以下行(以及其他不相关的函数):

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

require 'dbfunctions.php';

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

and my dbfunctions.php looks like this:

<?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文档中,我调用了以下函数:

In my PHP document I call the following function :

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

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

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

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

我在网页上使用了标签( http://jqueryui.com/tabs/#default ),即我想在哪里打电话. myAccountPage()函数给出以下错误:

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 :

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

推荐答案

我认为这是因为第一次关闭数据库连接时,您会忘记这样做:

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.

这篇关于警告:mysqli_query():无法获取mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆