0

now I learn write code in PHP.

I have a local database (phpmyadmin) named users_db.

I have a table of users in the database and cells id, user_name, user_email, user_password

If I want to read data from a database into a variable, I have written code like this.

public static function query ($sql, $parameters = array()){
    $query = self::$connection->prepare($sql);
    $query->execute($parameters);
    return $query;
}


public static function fetchData(){
   $query = Databes::query('SELECT * FROM `users`');
   $data = $query->fetchALL();
   return $data;
}

I store the function in a variable such as $x. If I use the print_r variable $x to get this result.

Array ( 
    [0] => Array ( 
        [id] => 1 
        [0] => 1 
        [user_name] => John 
        [1] => John 
        [user_email] => john@email.com 
        [2] => john@email.com 
        [password] => newC 
        [3] => newC
    )

Can someone please explain to me why the statement is duplicating?

[id] => 1;
[0] => 1; 

Is that right? My goal is not to unnecessarily burden the server with a lot of data.

Barmar
  • 669,327
  • 51
  • 454
  • 560
  • The default fetch mode is `PDO::FETCH_BOTH`. Use `PDO::FETCH_ASSOC` if you just want the named entries, `PDO::FETCH_NUM` if you want the numbered entries. – Barmar Feb 22 '22 at 11:33

0 Answers0