Harry Harry - 4 months ago 7
MySQL Question

Load mySQL results in arrays into a array

I can't figure out how to load the mySQL results (address,name) into this format:

$to=array(
array("address"=>"johndoe@domain.ext","name"=>"John Doe"),
array("address"=>"jofrost@domain.ext","name"=>"Jo Frost"),
array("address"=>"someone@domain.ext","name"=>"Some One"),
etc...
);


Please the old way for learning.
Thx.

Answer

like that

$query = 'SELECT address, name FROM table';
$mysqli = new mysqli('host', 'user', 'password', 'database');

if ($mysqli->connect_errno) {
    echo 'Connect failed:' . $mysqli->connect_error;
    exit();
}

if($res = $mysqli->query($query)) {
    $ret = [];
    while($row = $res->fetch_assoc()) {
       $ret[] = $row;
    }

    print_r($ret);

 } else {
      echo $mysqli->error;
 }

see more:

http://php.net/manual/en/book.mysqli.php

http://php.net/manual/en/mysqli.query.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php