如何通过php中的mysql查询逐行迭代 [英] How to iterate by row through a mysql query in php

查看:137
本文介绍了如何通过php中的mysql查询逐行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试查询我的数据库并选择具有特定值的所有行。
之后我将查询转换为带有mysql_fetch_array()的数组,然后我尝试使用for for循环逐行遍历获取的数组。

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'];
}
?>

虽然当我为pointsAvailable列之外的任何列执行此操作时,请说明名为name的列它只返回一个字母。

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?

推荐答案

$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将允许您使用命名列

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"]);
}

这篇关于如何通过php中的mysql查询逐行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆