【发布时间】:2013-10-14 17:38:07
【问题描述】:
在任何人判断之前...我已经检查了其他有类似问题但仍然无法解决我的问题的帖子。
php mysqli only returns one row
Fetching one row only with MySQLi
我还检查并阅读了 PHP 手册,使用不同的方法来提取数据没有运气。 我的意思是我确实提取了一个表,该表甚至具有正确的行数。但是,所有行都具有相同的信息:第一行,即表示标题的行,即:名字、电话、地址等。
下面的代码只是展示了我是如何做的。我重申,mysqli 连接确实是在提取包含数据的表,并且具有正确的行数。但我没有得到正确的信息。
public function queryexec()
{
if(isset($this->query))
{
//execute prepared query and store in result variable.
$this->result = $this->connection->query($this->query);
echo 'Table Row Count: '. $this->result->num_rows;
echo '<br/>';
//fetch_array gives me table but only headers but correct amount of rows.
//Fetch_object doesnt work throws me error:
//Fatal error: Cannot use object of type stdClass as array
while($row = $this->result->fetch_array()){
printf ("%s (%s)\n", $row[0], $row[2][1]);
//echo 'Table Data: '. $newdata["LastName"];
//echo '<br/>';
}
return true;
}
return false;
}
【问题讨论】:
-
尝试使用
while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {:php.net/manual/en/pdostatement.fetch.php -
我想 $stmt 是查询的结果,对吧?但它给了我一个错误:致命错误:调用未定义的方法mysqli_result::fetch()
-
是的,在你的情况下,你会使用
$this->fetch_object或$this->fetch_assoc: php.net/manual/en/mysqli-result.fetch-assoc.php -
它没有工作,所以我最好的选择是尝试 PDO api 来完成这个任务。
-
也试过 $this->result->fetch_object 吗?祝你好运:)