1

I've got a function that gets data from a NoSQL database, and if it's not available then it goes to a MySQL database to get the data but the issue is, the function is putting the data into the array twice and I can't figure out why.

Expected result

array(2) { 
["id"]=> string(2) "30" 
["username"]=> string(8) "Username" }

Actual result

array(4) { 
[0]=> string(2) "30" 
["id"]=> string(2) "30" 
[1]=> string(8) "Username" 
["username"]=> string(8) "Username" }

Code

Code that is irrelevant to the problem is removed and replaced by pseudo code comments.

        $Connection = $this->Connect("MySQLi");
        $Data = MySQLi_Fetch_Array(

            MySQLi_Query($Connection["MySQLi"], $Options["Query"])

        );

        echo "Got array (MySQLi).";

It's worth noting that the string "Got array (MySQLi)." only appears once.

4

2 回答 2

2

MySQLi_Fetch_Array在一个数组中获取重复数据 - 同时具有数字和关联索引

mysqli_fetch_assoc改为使用仅具有关联性

于 2013-07-21T18:19:55.513 回答
1

mysqli_fetch_array()接受一个参数,resulttype默认情况下设置为MYSQLI_BOTH

通过使用 MYSQLI_ASSOC 常量,该函数的行为与 mysqli_fetch_assoc() 相同,而 MYSQLI_NUM 的行为与 mysqli_fetch_row() 函数相同。最后一个选项 MYSQLI_BOTH 将创建一个具有两者属性的数组。

mysqli_fetch_assoc获取关联数组,同时mysqli_fetch_row获取具有数字索引的数组。

mysqli_fetch_array使用该参数MYSQLI_BOTH将获取同一数组中的命名(关联)索引和数字索引。

于 2013-07-21T18:23:56.473 回答