简体   繁体   中英

Why data retrieved from database was added to php array two times?

I have a simple user table with columns: username, password, email, name, surname, birthdate, address and city. the first three are mandatory but the rest of them can be null. I am connecting to the database and fetch rows in an array and print it.

$dbc=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "select * from user"; 
$data = mysqli_query($dbc, $sql) or die("query exec error");
//$json = array();
if(mysqli_num_rows($data)){
    while($row=mysqli_fetch_array($data)){
        //$json['Kullanici'][]=$row;
        print_r($row);
        echo "<br /><br />";
    }
}
mysqli_close($dbc);
//echo json_encode($json);

But the output is like this:

Array ( [0] => ayse [username] => ayse [1] => b1b3773a05c0ed0176787a4f1574ff0075f7521e [password] => b1b3773a05c0ed0176787a4f1574ff0075f7521e [2] => ayse@hotmail.com [email] => ayse@hotmail.com [3] => [name] => [4] => [surname] => [5] => 0000-00-00 [birthdate] => 0000-00-00 [6] => [address] => [7] => istanbul [city] => istanbul )

Array ( [0] => baris [username] => baris [1] => 7c4a8d09ca3762af61e59520943dc26494f8941b [password] => 7c4a8d09ca3762af61e59520943dc26494f8941b [2] => bakkurt@hotmail.com [email] => bakkurt@hotmail.com [3] => Barış [name] => Barış [4] => [surname] => [5] => 0000-00-00 [birthdate] => 0000-00-00 [6] => [address] => [7] => [city] => ) 

The question is why there is both [0]=>baris and [username] => baris. I was expecting only the username=>baris. Was I wrong? Where am i missing? If i solve this problem, I will convert the array to json by removing comments.

It's not a problem, if you look at the PHP documentation for the mysqli_fetch_array() function, it accepts a parameter resulttype ; it determines whether to return records as arrays with numeric indices alone, associative indices alone or both (which is what you have).
By default, the function uses MYSQLI_BOTH .
To get as numeric indices alone:

while($row=mysqli_fetch_array($data, MYSQLI_NUM)){
    //this will always return with numeric indices alone
}

For more information check here PHP mysqli_fetch_array

if you need only username => baris

Try mysqli_fetch_assoc .

Courtesy - http://www.php.net

It returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.

I think what you are looking for is the resulttype parameter. Have a look at this page: mysqli_result::fetch_array

You probably want to call the fetch_array function like this:

mysqli_fetch_array($data, MYSQLI_ASSOC)

Try using mysqli_fetch_assoc() instead of mysqli_fetch_array() .

mysqli_fetch_array() fetch data as both enumerated and associative, so if you only need associative array use mysqli_fetch_array() .

You can also read the documentation if you want to know something more about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM