用mysqli结果获取行数组 [英] get array of rows with mysqli result
本文介绍了用mysqli结果获取行数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从结果对象获取所有行.我正在尝试建立一个可以容纳所有行的新阵列.
I need to get all the rows from result object. I’m trying to build a new array that will hold all rows.
这是我的代码:
$sql = new mysqli($config['host'],$config['user'],$config['pass'],$config['db_name']);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT domain FROM services";
$result = $sql->query($query);
while($row = $result->fetch_row());
{
$rows[]=$row;
}
$result->close();
$sql->close();
return $rows;
$rows
应该是包含所有行的新数组,但是我得到一个空数组.
$rows
is supposed to be the new array that contains all, rows but instead I get an empty array.
有任何想法为什么会发生这种情况?
Any ideas why this is happening?
推荐答案
您有一个轻微的语法问题,即错误的分号.
You had a slight syntax problem, namely an errant semi-colon.
while($row = $result->fetch_row());
注意分号结尾吗?这意味着后面的程序段不是循环执行的.摆脱它,它应该可以工作.
Notice the semi-colon at the end? It means the block following wasn't executed in a loop. Get rid of that and it should work.
此外,您可能需要检查查询是否确实有效:
Also, you may want to check that the query actually works:
$sql = new mysqli($config['host'], $config['user'], $config['pass'], $config['db_name']);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
$query = "SELECT domain FROM services";
$result = $sql->query($query);
if (!$result) {
printf("Query failed: %s\n", $mysqli->error);
exit;
}
while($row = $result->fetch_row()) {
$rows[]=$row;
}
$result->close();
$sql->close();
return $rows;
这篇关于用mysqli结果获取行数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文