0

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

$to=array(
    array("address"=>"[email protected]","name"=>"John Doe"),
    array("address"=>"[email protected]","name"=>"Jo Frost"),
    array("address"=>"[email protected]","name"=>"Some One"),
    etc...
);

Please the old way for learning. Thx.

4
  • 1
    It would be helpful to see what code you have currently and what it outputs. Commented Jul 31, 2016 at 20:55
  • How are you getting this ? Commented Jul 31, 2016 at 21:00
  • Do you want to load data into this format from dbase? If so its just fetchAll in all dbase providers. Commented Jul 31, 2016 at 21:00
  • It's a demo script I like to adjust, so the code above is inside but I have my data in a mySQL database. Commented Jul 31, 2016 at 21:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks... but the only output I got is: Array ( )
How do you do a database query?
What do you mean? I just test your code copy/paste with, ofcourse, the right variables for table, database etc.
make another copy/paste
I found out: you forgot the () behind fetch_assoc, it works, thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.