MySQL mysql_fetch_row() 与 mysql_fetch_assoc() 与 mysql_fetch_array()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11480129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_fetch_row() vs mysql_fetch_assoc() vs mysql_fetch_array()
提问by user1479431
Possible Duplicate:
mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object
I want a function which can return fields in the current row of a resultset into an associative array and moves the result pointer to the next row.
我想要一个可以将结果集当前行中的字段返回到关联数组并将结果指针移动到下一行的函数。
Confused between
迷茫之间
mysql_fetch_row()
mysql_fetch_assoc()
mysql_fetch_array()
Which one should I use? Any help would be appreciated. Thank you.
我应该使用哪一种?任何帮助,将不胜感激。谢谢你。
回答by Filip Roséen - refp
Note: The use of the
mysql_*
functions are considered deprecatedand you should instead use something that offers better security and more functionality, such as MySQLior PDO.
What is it?
它是什么?
You are looking for mysql_fetch_assoc
,as the name implies it will return an associative array (with the column names as keys and the values as the row values).
您正在寻找mysql_fetch_assoc
,顾名思义,它将返回一个关联数组(列名作为键,值作为行值)。
What will the different functions return?
不同的函数会返回什么?
All of the mentioned functions will return an array, the differences between them is what values that are being used as keys in the returned object.
所有提到的函数都将返回一个数组,它们之间的区别在于哪些值被用作返回对象中的键。
This function will return a row where the values will come in the order as they are defined in the SQL query, and the keys will span from
0
to one less than the number of columns selected.This function will return a row as an associative array where the column names will be the keys storing corresponding value.
This function will actually return an array with both the contents of
mysql_fetch_row
andmysql_fetch_assoc
merged into one. It will both have numericand stringkeys which will let you access your data in whatever way you'd find easiest.It is recommended to use either
_assoc
or_row
though.
此函数将返回一行,其中的值将按照 SQL 查询中定义的顺序出现,并且键的范围
0
将小于所选列数。此函数将作为关联数组返回一行,其中列名将是存储相应值的键。
此功能会实际上两者的内容返回一个数组
mysql_fetch_row
,并mysql_fetch_assoc
合并成一个。它将同时具有数字键和字符串键,可让您以任何您认为最简单的方式访问您的数据。建议使用两种
_assoc
或_row
虽然。
回答by Niet the Dark Absol
mysql_fetch_assoc
when you are manually referring to the fields.
mysql_fetch_assoc
当您手动引用字段时。
mysql_fetch_row
when you are using it as part of a list($a,$b,$c...) = ...
mysql_fetch_row
当您将其用作 list($a,$b,$c...) = ...
Never mysql_fetch_array
.
从来没有mysql_fetch_array
。
回答by cyclotrojan
mysql_fetch_assoc()
mysql_fetch_assoc()
Say your table has two columns id and name. You can use the following snippet -
假设您的表有两列 id 和 name。您可以使用以下代码段 -
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
echo $row["name"];
}