如何在 php 中按行迭代 mysql 查询

发布于 2024-08-22 03:54:42 字数 471 浏览 8 评论 0原文

好的,所以我尝试查询我的数据库并选择具有特定值的所有行。 之后,我使用 mysql_fetch_array() 将查询转换为数组,然后尝试使用 foreach 循环按行迭代获取的数组。

<?php
$query = mysql_query("SELECT * FROM users WHERE pointsAvailable > 0 ORDER BY pointsAvailable Desc");
$queryResultArray = mysql_fetch_array($query);
foreach($queryResultArray as $row)
{
    echo $row['pointsAvailable'];
}
?>

尽管当我对pointsAvailable 列之外的任何列执行此操作时,例如名为“name”的文本类型列,它只返回一个字母。

如何逐行迭代返回的查询,并允许从当前行获取特定的数据列?

Ok, So I am trying to query my database and select all rows that have a certain value.
After that I turn the query into an array with mysql_fetch_array(), then I tried iterating by row through the fetched array using a for each loop.

<?php
$query = mysql_query("SELECT * FROM users WHERE pointsAvailable > 0 ORDER BY pointsAvailable Desc");
$queryResultArray = mysql_fetch_array($query);
foreach($queryResultArray as $row)
{
    echo $row['pointsAvailable'];
}
?>

Though when I do this for any column besides the pointsAvailable column say a column named "name" of type text it only returns a single letter.

How do I iterate through a returned query row by row, and be allowed to fetch specific columns of data from the current row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

演多会厌 2024-08-29 03:54:42
$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

或者使用 MYSQL_ASSOC 将允许您使用命名列

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}
$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

or using MYSQL_ASSOC will allow you to use named columns

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}
请叫√我孤独 2024-08-29 03:54:42

是的,使用 mysql_fetch_array($result) 是正确的方法。

Yes using mysql_fetch_array($result) is the way to go.

只是一片海 2024-08-29 03:54:42

随着 PHP 7+ 成为新标准并且 mysql_fetch_array 被弃用,您将需要开始使用 mysqli_fetch_array

面向对象风格

$result = $mysqli->query($query);
/* numeric array */
while ($row = $result->fetch_array(MYSQLI_NUM)) {
    // Do stuff with $row
}
/* associative array */
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
    // Do stuff with $row
}
/* associative and numeric array */
while ($row = $result->fetch_array(MYSQLI_BOTH)){
    // Do stuff with $row
}

过程风格

$result = mysqli_query($mysqli, $query);
/* numeric array */
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    // Do stuff with $row
}
/* associative array */
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    // Do stuff with $row
}
/* associative and numeric array */
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
    // Do stuff with $row
}

您可以在 PHP Bible 上阅读有关此更改的更多信息。

https://www.php.net/manual/en/mysqli-result.fetch-array.php

With PHP 7+ becoming the new standard and mysql_fetch_array becoming deprecated you will need to start using mysqli_fetch_array.

Object-oriented style

$result = $mysqli->query($query);
/* numeric array */
while ($row = $result->fetch_array(MYSQLI_NUM)) {
    // Do stuff with $row
}
/* associative array */
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
    // Do stuff with $row
}
/* associative and numeric array */
while ($row = $result->fetch_array(MYSQLI_BOTH)){
    // Do stuff with $row
}

Procedural style

$result = mysqli_query($mysqli, $query);
/* numeric array */
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    // Do stuff with $row
}
/* associative array */
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    // Do stuff with $row
}
/* associative and numeric array */
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
    // Do stuff with $row
}

You can read more about this change on the PHP Bible..

https://www.php.net/manual/en/mysqli-result.fetch-array.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文