【问题标题】:Is mysql_fetch_array a the combination of mysql_fetch_assoc and mysql_fetch_row?mysql_fetch_array 是 mysql_fetch_assoc 和 mysql_fetch_row 的组合吗?
【发布时间】:2013-09-04 07:57:04
【问题描述】:

我一直在阅读 mysql_fetch_* 方法。 这是我从 PHP.org 网站了解到的。

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc — Fetch a result row as an associative array
mysql_fetch_object — Fetch a result row as an object
mysql_fetch_row — Get a result row as an enumerated array

看起来 mysql_fetch_array 包含了所有存在于

中的值

mysql_fetch_assoc,mysql_fetch_object,mysql_fetch_row。因为mysql_fetch_assoc只包含关联数组,

mysql_fetch_row 包含数字数组中的数据。

mysql_fetch_object 也返回关联数组。

请告诉我我的理解是正确还是错误。

【问题讨论】:

  • 您是正确的,除了 mysql_fetch_object 部分。 mysql_fetch_object 返回一个对象,其列名是该对象的属性。 mysql_fetch_array 没有这种类型的东西
  • 是的,你基本上是在正确的轨道上。但请注意,所有mysql_xxx() 函数均已弃用,不建议使用;如果您正在学习 PHP,则应该学习更现代的 API,例如 PDO。

标签: php mysql database arrays


【解决方案1】:

mysql_fetch_assoc 返回一个关联数组,mysql_fetch_row 返回一个数值数组,mysql_fetch_array 可以选择输出。这个函数接受一个可以取值的可选参数:

  • MYSQL_ASSOC - 返回关联数组
  • MYSQL_NUM - 返回数值数组
  • MYSQL_BOTH - 返回组合的数字和关联数组

最后一个值是默认值。

mysql_fetch_object 略有不同,因为它返回一个对象,该对象具有与从数据库中获取的结果中的列相对应的字段。

作为旁注,我想补充一点,mysql_* 函数已被弃用,您应该切换到 mysqli 或 PDO。

【讨论】:

    猜你喜欢
    • 2012-07-13
    • 2023-03-23
    相关资源
    最近更新 更多