问题描述
快速问题...我的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):
如果结果的两个或更多列具有相同的字段名称,则最后一列优先.要访问相同名称的其他列,您需要使用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,查询仅返回一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!