Fetch all rows based on the query into an array

前端 未结 2 1984
萌比男神i
萌比男神i 2021-01-17 01:03

I have the following code:

  //--------------------------------------------------------------------------
  // 2) Query database for data
  //---------------         


        
2条回答
  •  一整个雨季
    2021-01-17 01:44

    $result = mysql_query("SELECT * FROM $tableName");  
    $arr_json = array();
    while ($row = mysql_fetch_assoc($result)) {
       $json = json_encode($row);
       $arr_json[] = $json;
    }
    

    EDIT: Looking a j08691's answer, it looks like I might have misunderstood.

    Anyway, if you don't know how many columns you have, do this:

    $arr = array();
    while ($row = mysql_fetch_assoc($result)) {
       $arr2 = array();
       foreach ($row as $val) $arr2[] = $val;
       $arr[] = $arr2;
    }
    

提交回复
热议问题