PHP-从查询结果中获取对象数组

| 问题的简化版: 因此,我在函数内部有此查询。
$query = \"SELECT * FROM users\";
$result = mysql_query($query);
我想如何使用mysql_fetch_object()从一行中获取一个对象。 因为最后我想获得一个对象数组,所以我这样做:
while ($a[] = mysql_fetch_object($result)) { // empty here }
最后,该函数仅返回$ a。它几乎可以正常工作。 我的问题是mysql_fetch_object将在最后返回NULL \“ row \”(这是正常的,因为结果结束了,但我仍将其分配给了数组)。 关于如何以体面的方式做到这一点的任何想法?提前致谢。     
已邀请:
        或者,您可以将任务从while条件移动到while主体,例如:
<?php
while ($entry = mysql_fetch_object($result)) {
   $a[] = $entry;
}
    
        如果没有更多行,则
mysql_fetch_object
实际上会返回
FALSE
。我会这样做:
$a = array();
while (($row = mysql_fetch_object($result)) !== FALSE) {
  $a[] = $row;
}
    
        您可以添加
array_pop($a);
while
之后 http://php.net/manual/zh/function.array-pop.php     
        这个问题似乎重复如何在php mysql中获取结果的所有行?
//Database Connection
$sqlConn =  new mysqli($hostname, $username, $password, $database);

//Build SQL String
$sqlString = \"SELECT * FROM my_table\";

//Execute the query and put data into a result
$result = $this->sqlConn->query($sqlString);

//Copy result into a associative array
$resultArray = $result->fetch_all(MYSQLI_ASSOC);

//Copy result into a numeric array
$resultArray = $result->fetch_all(MYSQLI_NUM);

//Copy result into both a associative and numeric array
$resultArray = $result->fetch_all(MYSQLI_BOTH);
    

要回复问题请先登录注册