PHP SQL,查询仅返回一行数据 [英] PHP SQL, query returns only one row of data

查看:36
本文介绍了PHP SQL,查询仅返回一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题...我的sql查询正在吐出这些数据...

quick question... my sql query is spitting out this data...

Array ( [id] => 1 [name] => Test Name [text] => This is text [image] => image.jpg ) 

当有两行数据时,我的php代码是否有问题?

When there are two rows of data, is there something wrong with my php code?

$query = "SELECT id, name, text, image FROM categories";
    $results = mysql_query($query, $connection);
    $results = mysql_fetch_assoc($results);

推荐答案

mysql_fetch_assoc一次获取一行.您需要遍历结果集:

mysql_fetch_assoc fetches one row at a time. You need to loop over the result set:

while(false !== ($row = mysql_fetch_assoc($results))){
    [handle $row here]
}

从文档中(搜索是您的朋友):

From the docs (search is your friend):

返回与提取的行相对应的字符串关联数组,如果没有更多行,则返回FALSE.

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

如果结果的两个或更多列具有相同的字段名称,则最后一列优先.要访问相同名称的其他列,您需要使用mysql_fetch_row()来访问带有数字索引的结果,或者添加别名.请参见有关别名的mysql_fetch_array()描述中的示例.

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row() or add alias names. See the example at the mysql_fetch_array() description about aliases.

欢呼

这篇关于PHP SQL,查询仅返回一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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