简体   繁体   中英

mysqli and php fetch object

I have the following code:

$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;
 }

How can I get the results into a array so I can do something like

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

I'm assuming you mean get all the rows in one array

$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;

Or, if you wanted the fields in the array:

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

you are using $results_latest->fetch_ object method
how do you think what method should be used to get an array ?

mysql_fetch_assocmysql_fetch_array

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

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

 while($row = $results_latest->fetch_array())
 {
  echo $row[0];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM