Return all data to a mysql_fetch_assoc

Asked

Viewed 238 times

1

I have a query that returns the data below:

nome -------- vencimento   
Joao -------- 09/08/2012   
Maria ------- 04/12/2015

That is to say, two records.

However, by putting it in PHP code only the top record is returned, even using += and a structure while in the mysql_fetch_assoc.

PHP code

 $result = array();


    $sql_select = mysql_query(
        "SELECT nome, vencimento
        FROM alunos"
    )or die(mysql_error());

    while($l = mysql_fetch_assoc($sql_select)){
        $result += $l;        
    }



return json_encode($result);

Return current $result:

{"nome":"Joao","vencimento":"09/08/2012"}

Expected return $result:

   {"nome":"Joao","vencimento":"09/08/2012"}
   {"nome":"Maria","vencimento":"04/12/2015"}

1 answer

0


+= does not work to add an item to an array, assign it with square brackets.

Change:

while($l = mysql_fetch_assoc($sql_select)){
    $result += $l;        
}

To:

while($l = mysql_fetch_assoc($sql_select)){
    $result[]= $l;        
}
  • It worked @rray, but you can tell me why it happens?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.