2012-02-22 119 views
0

我試圖創建動態象下面這樣使用PHP創建陣列動態

$data = array(
    array("date" => "1/2/2012", "sentstatus" => "0", "mobile" => "14578998"), 
     array("date" => "21/2/2012", "sentstatus" => "1", "mobile" => "14668998"), 
     array("date" => "1/5/2012", "sentstatus" => "1", "mobile" => "14598998"), 
     array("date" => "1/6/2012", "sentstatus" => "0", "mobile" => "14578748"), 
); 

下面的數組是插入SQL Server數據到數組我的PHP代碼,但問題是,它的陣列中只形成數據庫表的最後結果集行。我沒有得到這個想法插入所有數據庫錶行到數組如上圖所示:

$sql = "SELECT [start_date_time],[sent_status],[mobile_number] ,[play_file] 
     FROM [slice].[dbo].[tbl_message_detail] "; 
$res = odbc_exec($con,$sql) or die(odbc_error()); 
$rows = odbc_num_rows($res); 

while($row = odbc_fetch_array($res)) 
    {  
$data = array(
     array("Date_Time" => $row['start_date_time'], "Send_Status" => $row['sent_status'], "Mobile_Number" => $row['mobile_number'], "play_file" => $row['play_file']) 
    ); 
    } 
+0

改變'$ data'到'$數據[]' – diEcho 2012-02-22 06:22:03

回答

1

由於你創建與每個迭代一個新的數組只得到了最後一排。在while循環之外聲明$data

1

試試這個代碼:

while($row = odbc_fetch_array($res)) 
{  
    $data[] = array("Date_Time" => $row['start_date_time'], 
        "Send_Status" => $row['sent_status'], 
        "Mobile_Number" => $row['mobile_number'], 
        "play_file" => $row['play_file']); 
} 
+0

感謝很快回復。感謝您的簡單解決方案..它解決了我的問題 – 2012-02-22 06:19:20

1

你在每一輪循環的覆蓋$ data變量。這:

$data = array(); 
while($row = odbc_fetch_array($res)) 
{  
$data[] = array("Date_Time" => $row['start_date_time'], 
       "Send_Status" => $row['sent_status'], 
       "Mobile_Number" => $row['mobile_number'], 
       "play_file" => $row['play_file'] 
      ); 
} 

應該工作,因爲你需要