与mysqli结果获取一排行
我需要从结果对象中获取所有行。我正在尝试构建一个可以容纳所有行的新数组。
这是我的代码:
$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
应该是包含全部的新数组,而是我得到一个空数组。
有什么想法,为什么会发生这种情况?
I need to get all the rows from result object. I’m trying to build a new array that will hold all rows.
Here is my code:
$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
is supposed to be the new array that contains all, rows but instead I get an empty array.
Any ideas why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到了一个略有语法问题,即半柱错误。
注意最后的半彩色?这意味着块之后没有在循环中执行。摆脱它,应该起作用。
另外,您可能需要要求Mysqli报告遇到的所有问题:
You had a slight syntax problem, namely an errant semi-colon.
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 ask mysqli to report all problems it encountered:
MySQLI的最新版本具有一些可以简化此类任务的改进。
首先,有一个有用的功能可以返回与查询返回的所有行的数组“ rel =“ nofollow noreferrer”>
mysqli_fetch_all()
这意味着,如果您需要一个简单的枚举数组,则代码将要简单得多:
或者全部一行,
但是,如果您需要使用一些列来索引所得的数组,则仍然需要这样的循环:
请注意,请注意在填充阵列之前,您应该始终初始化数组,因为可能已经存在这样的变量。
另外,
mysqli_result
traverable 。这意味着您可以立即在foreach循环中使用它,就好像它是一个数组包含数据库中的所有行:但是在while循环中,它实际上只是 santtax sugar - 您无法访问的值
$ result
直接“阵列”。必须说明。
这个问题是十年的历史,并且在问题和接受的答案中进行了连接和查询的方式都被淘汰和皱眉。
建立连接时,有几件事要牢记。我在
die(mysqli_connect_error())
当要运行查询时,也有几件事要牢记:
mysqli_query()
mysqli_stmt_get_result()
应使用以使用熟悉的获取功能以获取产生的行。如果此功能不可用,则您可能会在CPANEL中勾选一些复选框(查找一个标记的mysqlnd
)。die(mysqli_error())
)。由于正确的错误模式,MySQLI将自动报告所有错误。Newest versions of mysqli have some improvements that can simplify such a task.
First, of all, there is a useful function to return an array with all rows returned by a query,
mysqli_fetch_all()
It means in case you need a simple enumerated array, the code would be much simpler:
or even all in one line,
However, if you need to use some column to index the resulting array, you still need a loop like this:
Note that you should always initialize an array before filling it up, because such a variable could already exist.
Also,
mysqli_result
class is now Traversable. It means you can use it in the foreach loop right away, as though it's an array contains all rows from the database:But it is effectively just a syntax sugar for the while loop - you cannot access values of
$result
"array" directly.Obligatory notes.
This question is a decade old, and the way a connection is made and the query is performed, both in the question and the accepted answer, are obsoleted and frowned upon nowadays.
When a connection is made, there are several things to keep in mind. I wrote an article on how to connect with mysqli properly that provides a correct connection example emphasizing on the following issues:
die(mysqli_connect_error())
)When it goes to running a query, there are several things to keep in mind as well:
mysqli_query()
mysqli_stmt_get_result()
should be used in order to use familiar fetch functions to get the resulting rows. In case this function is not available you are probably to tick some checkbox in your cpanel (look for one labeledmysqlnd
).die(mysqli_error())
). Thanks to the proper error mode, mysqli will report all errors automatically.