mysql_fetch_row() vs mysql_fetch_assoc() vs mysql_fetch_array()(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_rowand 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.
这篇关于mysql_fetch_row() 与 mysql_fetch_assoc() 与 mysql_fetch_array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql_fetch_row() 与 mysql_fetch_assoc() 与 mysql_fetch_array()
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
