mysqli - fetch_Array错误调用成员函数fetch_array()对非对象mysqli [英] mysqli - fetch_Array error call to a member function fetch_array() on a non-object mysqli

查看:141
本文介绍了mysqli - fetch_Array错误调用成员函数fetch_array()对非对象mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mysqli的新手,开始学习基本的东西。关于这个例子( http://php.net/manual/ en / mysqli-result.fetch-array.php )我试图fetch_array。这是我的代码。

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.

$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();

这里我得到这个错误调用一个非对象的成员函数fetch_array旁边同时条件线。我试图谷歌搜索,没有得到结果我的问题。希望我的问题很清楚。

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.

推荐答案

在运行查询时始终检查错误。

请注意,用不必要的长变量来延伸代码

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

看,第一个变量只包含SQL代码,它将被放置在下一行。

第二个变量包含mysqli结果。没有什么特别的意义了。可以使用常规名称。

同样适用于临时 $ row 变量。

唯一具有特殊意义的变量在你的代码是 $ arrChartData [] - 所以,给它有意义的名称。

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 $row variable.
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.

注意 trigger_error 部分会将mysqli错误转换为PHP错误。总是以这种方式运行你的查询,以通知所有的mysql错误

And note the trigger_error part which will convert mysqli error into PHP error. Always run your queries this way, to be notified of all mysql errors

顺便说一下,摆脱所有临时变量是一个好习惯,帮助函数,使您的应用程序代码如下面两行一样简单

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.

这篇关于mysqli - fetch_Array错误调用成员函数fetch_array()对非对象mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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