-1

i'm trying to create an array with PHP and MySQL, but i always get errors.

The code i'm using

function db_listar_usuarios(){
$link=db_connect();
$query = "select * from usuarios" or die("Problemas en el select: " . mysqli_error($link));
$result = $link->query($query);
  while($row = mysqli_fetch_assoc($result)) {   
        echo $row['nombre'] . array(;
        foreach ($row as $col => $val) {
           $col => $val;
        }
        echo "\n\n############\n";
    }
}

And what I want to create with this code is:

array(
    'john' => array('address' => 'st 123', 'age' => '25', 'surname' => 'doe'),
    'ane' => array('address' => 'av 456', 'age'=> '32', 'surname' => 'smith'),
);

To use then like something like this:

private $contacts = db_listar_usuarios();

Thank you in advance :)

2
  • 4
    but i always get errors. -- all our crystal balls are in the shop for repairs, so we can't magically read your mind to see what your errors are. Nov 7, 2013 at 17:25
  • Upvote that glorious comment. @AmalMurali Nov 7, 2013 at 18:33

2 Answers 2

1
function db_listar_usuarios(){
$link=db_connect();
$query = "select * from usuarios" or die("Problemas en el select: " . mysqli_error($link));
$result = $link->query($query);
  while($row = mysqli_fetch_assoc($result)) {   
        echo $row['nombre'] . array(; // <- invalid in several ways
        foreach ($row as $col => $val) {
           $col => $val;
        }
        echo "\n\n############\n";
    }
}

Try:

function db_listar_usuarios(){
    $link = db_connect();
    $query = "select * from usuarios" or die("Problemas en el select: " . mysqli_error($link));
    $result = $link->query($query);
    $myArray = array();
    while($row = mysqli_fetch_assoc($result)) {   
        $myArray[] = $row;
        print_r($myArray); // for debugging
        echo "\n\n############\n";
    }
    return $myArray;
}
3
  • thank you so much for your answer, but I can't make it work. i've got 2 files server.php -> pastebin.com/5j54m1Mz functions.php -> pastebin.com/N7jMhSBa I want to make an API with REST and with MySQL, but it doesn't work. Thanks you again!
    – ScaZ
    Nov 7, 2013 at 20:52
  • @ScaZ it just occurred to me that the code was doing needless work, and doing it wrong to boot. I've updated my code with something simpler that should work.
    – Sammitch
    Nov 7, 2013 at 21:25
  • Thank you so much, but it still not working... when I put: private $contacts = db_listar_usuarios(); PHP ERROR: Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp\htdocs\server.php on line 7 Thanks for your help!!
    – ScaZ
    Nov 7, 2013 at 22:07
1
$users = array();
while($row = mysqli_fetch_assoc($result)) {            
    foreach ($row as $col => $val) {
        $users[$col] = $val;
    }
}
print_r($users);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.