mysql_fetch_row() 与 mysql_fetch_assoc() 与 mysql_fetch_array()
可能的重复:
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.
混淆
mysql_fetch_row()
mysql_fetch_assoc()
mysql_fetch_array()
我应该使用哪个?任何帮助,将不胜感激.谢谢.
Which one should I use? Any help would be appreciated. Thank you.
推荐答案
注意:
mysql_*
函数的使用被视为不推荐使用,您应该改用提供更好安全性和更多功能,例如 MySQLi 或 PDO.
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_fetch_assoc
, 顾名思义,它将返回一个关联数组(列名作为键,值作为行值).
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).
所有提到的函数都会返回一个数组,它们之间的区别在于哪些值被用作返回对象中的键.
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
此函数将返回一行,其中的值将按照 SQL 查询中定义的顺序出现,键的范围将从 0
到小于所选列数的 1.
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.
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
这个函数实际上会返回一个数组,其中 mysql_fetch_row
和 mysql_fetch_assoc
的内容合并为一个.它将具有 numeric 和 string 键,可让您以任何您认为最简单的方式访问您的数据.
This function will actually return an array with both the contents of mysql_fetch_row
and mysql_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.
虽然建议使用 _assoc
或 _row
.
It is recommended to use either _assoc
or _row
though.
相关文章