You need to convert the generated string to an array

$carr = mysqli_query($db, "SELECT id,t,b,ty,msg,log FROM `comm` ORDER BY id DESC"); $comm = 'array( '; while ($row = mysqli_fetch_array($carr)) { $comm .= "array('id'=>$row[id], 't'=>$t, 'text'=>'$row[msg]'), "; } $comm .= ")"; print_r($comm); 

At the exit:

 array( array('id'=>4, 't'=>NULL, 'text'=>'11111111111111111111'), array('id'=>3, 't'=>NULL, 'text'=>'1111111111111'), array('id'=>2, 't'=>NULL, 'text'=>'Тестик'), ) 

Accordingly, it is defined as a string. how to be?

    2 answers 2

     $carr = mysqli_query($db, "SELECT id,t,msg as text FROM `comm` ORDER BY id DESC"); while ($row = mysqli_fetch_assoc($carr)) { $comm[] = $row; } print_r($comm); 

    or

     $carr = mysqli_query($db, "SELECT id,t,msg as text FROM `comm` ORDER BY id DESC"); while ($row = mysqli_fetch_array($carr, MYSQLI_ASSOC)) { $comm[] = $row; } print_r($comm); 
       $carr = mysqli_query($db, "SELECT id,t,b,ty,msg,log FROM `comm` ORDER BY id DESC"); while ($row = mysqli_fetch_array($carr)) { $comm[] = $row; } print_r($comm); 

      Also in the loop, the $t variable will always be NULL, $row['t'] will be correct

      • gives a little wrong, all values ​​are selected and an array of the form Array ( [0] => Array ( [0] => 4 [id] => 4 [1] => 0 ... and you need an array( array('id'=>1, 't'=>NULL, 'text'=>'Тестик'), array('id'=>2, 't'=>1, 'text'=>'Child'), ... - Rufex