为什么mysqli_fetch_array()返回的数组大小是原来的两倍?


Why does mysqli_fetch_array() return array double the size?

我写了一个非常简单的php语句,比如

print_r($my_rec);
$row = mysqli_fetch_array($my_rec);
echo "'nP_Id " . $row['P_Id'] . ' Length ' . sizeof($row) . "'n";
$row = mysqli_fetch_array($my_rec);
echo "'nP_Id " . $row['P_Id'] . ' Length ' . sizeof($row) . "'n";

现在,当print_r打印到屏幕时,它报告每个数组中只有16个字段/列。这是真的。然而,当我获取一个数组时,它会给我双倍的字段。换句话说,这个数组在重复自己,这是没有意义的。例子…

我的数据库中有

 Col1  Col2
 "hi1" "bye1"

当我做

 $row = mysqli_fetch_array($my_rec);
 foreach($row as $x){
       echo $x . "'n";
 }

输出

 hi1
 hi1
 by2
 by2

为什么我得到这个行为?

因为它还包含一个包含列位置的数组(即:0,1,2,3和id, username, password, email)。id0保存相同的数据

如果你只想要字符串索引,你可以使用mysqli_fetch_assoc (http://php.net/mysqli_fetch_assoc)