-1

当我加载包含以下代码的 PHP 页面时,我收到错误“调用非对象上的成员函数 fetch_object()”。它与以单词while开头的行有关。为什么会弹出这个错误?请忽略我的安全漏洞。

PHP 片段:

header('Content-Type:application/json;charset=utf-8');

$file_absolute = "---placeholder for correct file path---";
include_once($file_absolute);
$mysql = new mysqli($db_host, $db_username, $db_password, $db_name);
$verb_value = $_POST['verb_value'];

$mysql->query("SET CHARACTER SET 'utf8'");

$result = $mysql->query("SELECT present_tense FROM $verb_value");

$queryResult = array();

while ($row = $result->fetch_object())
{
    $queryResult[] = $row->present_tense;
}
4

2 回答 2

7

您的代码中缺少错误检查:

$result = $mysql->query("SELECT present_tense FROM $verb_value");
if( !$result)
  die($mysql->error);

$queryResult = array();

while ($row = $result->fetch_object())
{
    $queryResult[] = $row->present_tense;
}

您的查询结果是一个非对象,但您没有检查它。

注意:您的代码很容易发生 SQL 注入:$verb_value = $_POST['verb_value']导致在不检查的情况下将 SQL 代码注入数据库的可能性!

于 2012-09-23T13:08:03.507 回答
0
$verb_value = $_POST['verb_value'];

$mysql = new mysqli($db_host, $db_username, $db_password, $db_name);

$mysql->query("SET CHARACTER SET 'utf8'");

if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }
$query = "SELECT present_tense FROM $verb_value";
$queryResult = array();
if ($result = $mysql->query($query)) {
  while ($row = $result->fetch_object()) {
     $queryResult[]=$row->present_tense;
  }
  $result->close();
 }
于 2012-09-23T13:43:50.290 回答