php $mysqli->fetch_object($result) 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12209719/
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_object($result) not working
提问by Sangam254
I am learning mysqli.
我正在学习mysqli。
I am trying to fetch data from a table "tbllogin".
我正在尝试从“tbllogin”表中获取数据。
//DATABASE CONNECTION
$hostname="p:localhost";
$database="dbLogin";
$username="user1";
$password="pwd1";
$mysqli = new mysqli($hostname, $username, $password,$database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
// Create Query
$query = "SELECT * FROM tbllogin";
// Escape Query
$query = $mysqli->real_escape_string($query);
echo $query;
// Execute Query
if($result = $mysqli->query($query)){
print_r($result);
while($row = $mysqli->fetch_object($result)){
echo $row->column;
}
//Free result set
$result->close();
}
?>
But $mysqli->fetch_object($result)is not working. The ifstatement containing $mysqli->fetch_object($result)does not execute. I cannot identify if there is any error.
但$mysqli->fetch_object($result)不工作。if包含的语句$mysqli->fetch_object($result)不执行。我无法确定是否有任何错误。
Please help. Also, suggest whether mysqli procedural form is better or object-oriented form?
请帮忙。还有,请问mysqli程序形式好还是面向对象形式好?
Thanks in advance.
提前致谢。
回答by Terry Seidler
Shouldn't that be $result->fetch_object()?
不应该$result->fetch_object()吗?
http://php.net/manual/en/mysqli-result.fetch-object.php
http://php.net/manual/en/mysqli-result.fetch-object.php
From Example 1:
从示例 1:
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
According to the answers on this stackoverflow page, there is not much of a difference between the two.
根据this stackoverflow page上的答案,两者之间没有太大区别。
回答by Francis Bailey
Try this:
尝试这个:
if($result = $mysqli->query($query)){
print_r($result);
while($row = $result->fetch_object($result)){
//do something
}
}
You need to chain the commands together.
您需要将命令链接在一起。

