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
", 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.
知道为什么会这样吗?
推荐答案
你有一个轻微的语法问题,即一个错误的分号.
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.
另外,你可能想让mysqli报告它遇到的所有问题:
Also, you may want to ask mysqli to report all problems it encountered:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$sql = new mysqli($config['host'], $config['user'], $config['pass'], $config['db_name']);
$query = "SELECT domain FROM services";
$result = $sql->query($query);
$rows = [];
while($row = $result->fetch_row()) {
$rows[] = $row;
}
return $rows;
这篇关于获取带有 mysqli 结果的行数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取带有 mysqli 结果的行数组


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01