mysqli和php获取对象

Oli*_*ton 1 php oop mysqli

我有以下代码:

$sql_latest = "SELECT * FROM tbl_latest ORDER BY id DESC LIMIT 0,3 ";

 $results_latest = $mysqli->query($sql_latest);

 while($row = $results_latest->fetch_object())
 {
  echo $row->id;
 }
Run Code Online (Sandbox Code Playgroud)

如何将结果放入数组中,以便我可以执行类似的操作

echo $ row [1]; echo $ row [2]; echo $ row [2];

irc*_*ell 6

我假设你的意思是在一个数组中获取所有行

$sql_latest = "SELECT * FROM tbl_latest ORDER BY id DESC LIMIT 0,3 ";
$results_latest = $mysqli->query($sql_latest);
$rows = array();
while($row = $results_latest->fetch_object())
{
    $rows[] = $row;
}

echo $rows[0]->id;
echo $rows[1]->id;
Run Code Online (Sandbox Code Playgroud)

或者,如果您想要数组中的字段:

while ($row = $results_latest->fetch_array()) {
    echo $row[0];  //Prints the first column
}
Run Code Online (Sandbox Code Playgroud)