我有以下代码:
$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;
}
如何将结果输入数组,以便我可以执行类似
的操作echo $ row [1]; echo $ row [2]; echo $ row [2];
答案 0 :(得分: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;
或者,如果你想要数组中的字段:
while ($row = $results_latest->fetch_array()) {
echo $row[0]; //Prints the first column
}
答案 1 :(得分:1)
您正在使用$ results_latest-> fetch_ 对象方法
你怎么认为应该用什么方法来获得数组?
答案 2 :(得分:0)
mysql_fetch_assoc
或mysql_fetch_array
答案 3 :(得分:0)
$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];
}