与mysqli结果获取一排行

发布于 2025-02-12 22:29:39 字数 555 浏览 1 评论 0原文

我需要从结果对象中获取所有行。我正在尝试构建一个可以容纳所有行的新数组。

这是我的代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2025-02-19 22:29:39

您遇到了一个略有语法问题,即半柱错误。

while($row = $result->fetch_row());

注意最后的半彩色?这意味着块之后没有在循环中执行。摆脱它,应该起作用。

另外,您可能需要要求Mysqli报告遇到的所有问题:

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;

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 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;
比忠 2025-02-19 22:29:39

MySQLI的最新版本具有一些可以简化此类任务的改进。

首先,有一个有用的功能可以返回与查询返回的所有行的数组“ rel =“ nofollow noreferrer”> mysqli_fetch_all()
这意味着,如果您需要一个简单的枚举数组,则代码将要简单得多:

$query = "SELECT domain FROM services";
$result = $sql->query($query);     
return $result->fetch_all(MYSQLI_ASSOC);

或者全部一行,

return $sql->query("SELECT domain FROM services")->fetch_all(MYSQLI_ASSOC);

但是,如果您需要使用一些列来索引所得的数组,则仍然需要这样的循环:

$query = "SELECT id, domain FROM services";
$result = $sql->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
  $data[$row['id']] = $row;
}

请注意,请注意在填充阵列之前,您应该始终初始化数组,因为可能已经存在这样的变量。

另外, mysqli_result traverable 。这意味着您可以立即在foreach循环中使用它,就好像它是一个数组包含数据库中的所有行:

$query = "SELECT domain FROM services";
$result = $sql->query($query);
foreach ($result as $row) {
    echo $row['domain'];
}

但是在while循环中,它实际上只是 santtax sugar - 您无法访问的值$ result直接“阵列”。

必须说明。

这个问题是十年的历史,并且在问题和接受的答案中进行了连接和查询的方式都被淘汰和皱眉。

建立连接时,有几件事要牢记。我在

  • 必须设置适当的错误报告模式
  • 必须设置适当的字符集
  • 不应使用任何手动错误报告代码(例如die(mysqli_connect_error())
  • )单独的文件,然后仅包含在需要数据库交互的每个脚本中。如果在函数中使用数据库代码,则必须将连接变量作为函数参数传递。

当要运行查询时,也有几件事要牢记:

  • 当查询中使用一个变量时,a 准备的语句 必须而不是mysqli_query()
  • 结果,一种特殊功能,称为 mysqli_stmt_get_result() 应使用以使用熟悉的获取功能以获取产生的行。如果此功能不可用,则您可能会在CPANEL中勾选一些复选框(查找一个标记的mysqlnd)。
  • 给出了与MySqli的准备好的声明,尽管有强制性,但要写很多代码,建议使用 helper helper'> helper函数对于mysqli 这将自动执行大部分工作,并使MySQLI准备的语句是常规查询。
  • 不应使用无手动错误报告代码(例如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:

$query = "SELECT domain FROM services";
$result = $sql->query($query);     
return $result->fetch_all(MYSQLI_ASSOC);

or even all in one line,

return $sql->query("SELECT domain FROM services")->fetch_all(MYSQLI_ASSOC);

However, if you need to use some column to index the resulting array, you still need a loop like this:

$query = "SELECT id, domain FROM services";
$result = $sql->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
  $data[$row['id']] = $row;
}

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:

$query = "SELECT domain FROM services";
$result = $sql->query($query);
foreach ($result as $row) {
    echo $row['domain'];
}

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:

  • a proper error reporting mode must be set
  • a proper character set must be set
  • no manual error reporting code should be ever used (like die(mysqli_connect_error()))
  • a connection has to be made only once, in a separate file, and then just included into every script that needs a database interaction. in case a database code is used in a function, a connection variable must be passed in as a function parameter.

When it goes to running a query, there are several things to keep in mind as well:

  • when even a single variable is used in the query, a prepared statement must be used instead of mysqli_query()
  • as a result, a special function called 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 labeled mysqlnd).
  • given a prepared statement with mysqli, although being obligatory, takes a lot code to write, it is advised to use a helper function for mysqli that would perform most of work automatically and make a mysqli prepared statement a smooth as a regular query.
  • no manual error reporting code should be ever used (like die(mysqli_error())). Thanks to the proper error mode, mysqli will report all errors automatically.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文