php mysqli - fetch_Array 错误调用非对象 mysqli 上的成员函数 fetch_array()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14639965/
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
mysqli - fetch_Array error call to a member function fetch_array() on a non-object mysqli
提问by Vignesh Gopalakrishnan
I am new to mysqli and started trying to learn basic things. With respect to this i example (http://php.net/manual/en/mysqli-result.fetch-array.php) i was trying fetch_array. Here is my code.
我是 mysqli 的新手,并开始尝试学习基本的东西。关于这个我的例子(http://php.net/manual/en/mysqli-result.fetch-array.php)我正在尝试fetch_array。这是我的代码。
$sqlGetChartData    =   "SELECT date, ratepersqft, location 
                          FROM ratepersqft
                         WHERE project_id = 1";
$runGetChartData    =   $mysqli->query($sqlGetChartData);
while($rowGetChartData = $runGetChartData->fetch_array(MYSQLI_BOTH))
    $arrGetChartData[]  =   $rowGetChartData;
    print "<pre>";
    print_r($arrGetChartData);
    exit();
Here i am getting this error Call to a member function fetch_array() on a non-object on line next to while condition line. I tried googling it and did not get result for my problem. Hope my question is clear. Thanks in Advance.
在这里,我收到此错误 Call to a member function fetch_array() on an non-object on a non-object on line next to while condition line。我尝试使用谷歌搜索它并没有得到我的问题的结果。希望我的问题很清楚。提前致谢。
回答by Your Common Sense
Always check for errors when running a query.
And please, don't stretch your code with unnecessarily long variables
运行查询时始终检查错误。
并且请不要使用不必要的长变量来扩展您的代码
$arrChartData[] = array();
$sql = "SELECT date, ratepersqft, location FROM ratepersqft WHERE project_id = 1";
$res = $mysqli->query($sql) or trigger_error($mysqli->error."[$sql]");
while($row = $res->fetch_assoc()) {
    $arrChartData[] = $row;
}
Look, first variable contains just SQL code with no special meaning in your program, and it will be disposed on the very next line.
Second variable contains mysqli result. With no special meaning again. It's ok to use conventional name.
Same goes for the temporary $rowvariable.
The only variable that have special meaning in your code is $arrChartData[]- so, give it meaningful name. You need to initialize it before filling though.
看,第一个变量只包含在你的程序中没有特殊意义的 SQL 代码,它将被放置在下一行。
第二个变量包含 mysqli 结果。又没有什么特别的意思了。可以使用常规名称。
临时$row变量也是如此。
在您的代码中唯一具有特殊含义的变量是$arrChartData[]- 所以,给它一个有意义的名字。不过,您需要在填充之前对其进行初始化。
And note the trigger_errorpart which will convert mysqli error into PHP error. Always run your queries this way, to be notified of all mysql errors
并注意trigger_error将 mysqli 错误转换为 PHP 错误的部分。始终以这种方式运行您的查询,以便收到所有 mysql 错误的通知
By the way, it is good practice to get rid of all temporary variables, by moving them into some sort of helper function, making your application code as simple as following 2 lines
顺便说一句,通过将所有临时变量移动到某种辅助函数中来摆脱所有临时变量是一种很好的做法,使您的应用程序代码像以下两行一样简单
$sql = "SELECT date, ratepersqft, location FROM ratepersqft WHERE project_id = 1";
$arrChartData[] = dbGetAll($sql);
It will make your code shorter and more readable.
它将使您的代码更短且更具可读性。
回答by lc.
The query probably failed and mysqli::queryreturned FALSE. Therefore $runGetChartDatais not a mysqli_resultobject, but a boolean, which is why you are getting your error.
查询可能失败并mysqli::query返回 FALSE。因此$runGetChartData不是mysqli_result对象,而是 a boolean,这就是您收到错误的原因。
From the documentation:
从文档:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
失败时返回 FALSE。对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象。对于其他成功的查询,mysqli_query() 将返回 TRUE。

