Return all data in a mysql_fetch_assoc

1

I have a query that returns the data below:

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

That is, two records .

However, by putting it in PHP code only the top record is returned, even using += and a while structure in 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 $ result current :

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

Return $ expected result :

   {"nome":"Joao","vencimento":"09/08/2012"}
   {"nome":"Maria","vencimento":"04/12/2015"}
    
asked by anonymous 31.08.2017 / 15:36

1 answer

0

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

Change:

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

To:

while($l = mysql_fetch_assoc($sql_select)){
    $result[]= $l;        
}
    
31.08.2017 / 15:40