为什么我收到错误:
致命错误:调用未定义的方法 mysqli::fetch_object()
这是我的代码:
$mysqli = new mysqli($db_data_hostname, $db_data_username, $db_data_password, $db_data_dbname);
$query = "SELECT attName, attType, attOptions FROM form_constructor WHERE device_name='$deviceType';";
$result = $mysqli->query($query);
while($row = $mysqli->fetch_object($result)){
echo $row->attName;
echo $row->attType;
echo $row->attOptions;
}
为什么会出现错误?
您需要使用分配有
$result
的变量 $mysqli->query
。 阅读手册上的 MySQLi 对象方法
您的代码已修复。
$mysqli = new mysqli($db_data_hostname, $db_data_username, $db_data_password, $db_data_dbname);
$query = "SELECT attName, attType, attOptions FROM form_constructor WHERE device_name='$deviceType';";
$result = $mysqli->query($query);
while($row = $result->fetch_object()){
echo $row->attName;
echo $row->attType;
echo $row->attOptions;
}
您应该对从 $result 获得的 stmt 结果调用 fetch_object。 所以你的 while 循环看起来像:
while($row = $result->fetch_object()) {
你必须使用 mysqli 结果:
while($row = $result->fetch_object()){
echo $row->attName;
echo $row->attType;
echo $row->attOptions;
}