Mysqli试图获取非对象错误的属性

时间:2014-11-04 07:48:12

标签: php mysqli

我有这个脚本,用于从一个表中选择描述并将其插入另一个表。

<?php
/**
Report Errors
*/
error_reporting(E_ALL);

$mysqli = new mysqli("localhost", "root", "", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$result = $mysqli->query("select subscriber_number,subscriber_category from subscriber_choices");

/*
500 Records Here
*/

$i = 0;
while($row = $result->fetch_array())
{
$subscriber_number = $row['subscriber_number'];
$subscriber_category = $row['subscriber_category'];

$mysqli->query("select description from jobs_content where category = $subscriber_category order by rand() limit 1 ");

$text_message = $mysqli->query("select description from jobs_content where category = '$subscriber_category' order by rand() limit 1")->fetch_object()->description; 

$mysqli->query("insert into le_grand_out dest_msisdn,text_message,sender_name,service_id values ($subscriber_number,'$text_message',6789900,6388398399004)");

}

$result->close()

?>

当我运行此脚本时,我在此行上收到错误

$text_message = $mysqli->query("select description from jobs_content where category = '$subscriber_category' order by rand() limit 1")->fetch_object()->description; 

当我例如写

时有效
$text_message = $mysqli->query("select description from jobs_content where category = 'general' order by rand() limit 1")->fetch_object()->description; 

为什么我收到此错误

Notice: Trying to get property of non-object in /var/www/html/send_sms.php on line 26 

2 个答案:

答案 0 :(得分:1)

如果没有返回任何行,

mysqli_result::fetch_object()将返回null

因此,如果您的查询没有结果,那么您正在运行:

null->description

这是非对象的属性。

答案 1 :(得分:0)

似乎你查询没有返回任何尝试正确检查或使用的查询     if($ result)     {     //在这里获取结果     }