Possible Duplicate:
mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object可能重复: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.
我应该使用哪一个?任何帮助,将不胜感激。谢谢。
3 个解决方案
#1
44
Note: The use of the
mysql_*
functions are considered deprecated and you should instead use something that offers better security and more functionality, such as MySQLi or PDO.注意:mysql_ *函数的使用被认为已弃用,您应该使用提供更好安全性和更多功能的东西,例如MySQLi或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.
所有提到的函数都将返回一个数组,它们之间的差异是在返回的对象中用作键的值。
-
和mysql_fetch_row
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.此函数将返回一行,其中值将按SQL查询中定义的顺序排列,并且键的范围将比所选列的数量小0到1。
-
mysql_fetch_assoc
This function will return a row as an associative array where the column names will be the keys storing corresponding value.
此函数将返回一行作为关联数组,其中列名称将是存储相应值的键。
-
mysql_fetch_array
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 numeric and string keys which will let you access your data in whatever way you'd find easiest.该函数实际上将返回一个数组,其中mysql_fetch_row和mysql_fetch_assoc的内容合并为一个。它将具有数字和字符串键,可让您以最简单的方式访问数据。
It is recommended to use either
_assoc
or_row
though.建议使用_assoc或_row。
#2
4
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当你将它作为列表的一部分使用时($ a,$ b,$ c ...)= ...
Never mysql_fetch_array
.
永远不要mysql_fetch_array。
#3
1
mysql_fetch_assoc()
mysql_fetch_assoc()
Say your table has two columns id and name. You can use the following snippet -
假设您的表有两列id和名称。您可以使用以下代码段 -
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
echo $row["name"];
}
#1
44
Note: The use of the
mysql_*
functions are considered deprecated and you should instead use something that offers better security and more functionality, such as MySQLi or PDO.注意:mysql_ *函数的使用被认为已弃用,您应该使用提供更好安全性和更多功能的东西,例如MySQLi或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.
所有提到的函数都将返回一个数组,它们之间的差异是在返回的对象中用作键的值。
-
和mysql_fetch_row
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.此函数将返回一行,其中值将按SQL查询中定义的顺序排列,并且键的范围将比所选列的数量小0到1。
-
mysql_fetch_assoc
This function will return a row as an associative array where the column names will be the keys storing corresponding value.
此函数将返回一行作为关联数组,其中列名称将是存储相应值的键。
-
mysql_fetch_array
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 numeric and string keys which will let you access your data in whatever way you'd find easiest.该函数实际上将返回一个数组,其中mysql_fetch_row和mysql_fetch_assoc的内容合并为一个。它将具有数字和字符串键,可让您以最简单的方式访问数据。
It is recommended to use either
_assoc
or_row
though.建议使用_assoc或_row。
#2
4
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当你将它作为列表的一部分使用时($ a,$ b,$ c ...)= ...
Never mysql_fetch_array
.
永远不要mysql_fetch_array。
#3
1
mysql_fetch_assoc()
mysql_fetch_assoc()
Say your table has two columns id and name. You can use the following snippet -
假设您的表有两列id和名称。您可以使用以下代码段 -
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
echo $row["name"];
}