mysql_fetch_array返回重复数据

mk_*_*_89 6 php mysql

每次运行mysql_fetch_array时,都会返回一个带有重复值的数组,例如

Array
(
    [0] => 1
    [row_id] => 1
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}
Run Code Online (Sandbox Code Playgroud)

但我只想在数组中得到单个结果,我尝试过使用

mysql_fetch_array($data, MYSQL_ASSOC);
Run Code Online (Sandbox Code Playgroud)

但这没有区别.

new*_*rey 13

这是预期的功能mysql_fetch_array().如果您不想拥有"重复"并且只有关联数组,请mysql_fetch_assoc()改用.

例:

while ($row = mysql_fetch_assoc($data)) { ... }
Run Code Online (Sandbox Code Playgroud)

  • `mysql_fetch_array($ data,MYSQL_ASSOC);`将与mysql_fetch_assoc完全相同. (3认同)

Rub*_*wal 5

mysql_fetch_array 返回作为查询执行的响应返回的结果集,作为关联数组和数字数组。为了将结果集作为关联数组返回,您需要使用 mysql_fetch_assoc 函数。为了将结果集作为数字数组返回,您需要使用 mysql_fetch_row 函数。