我正在嘗試從名爲Merchant的表中獲取數據(M_Name)。無法從MySQL中的另一個表中獲取數據
以下是我的代碼:
<?php
$response = array();
$link = mysql_connect('localhost','root','') or die ('Could not connect: '.mysql_error());
mysql_select_db('ichop') or die ('Could not connect to database');
$result = mysql_query("select * from offer") or die(mysql_error());
if(mysql_num_rows($result) > 0){
$response["offers"] = array();
while($row = mysql_fetch_array($result)){
$offer = array();
$offer["offer_id"] = $row["Offer_ID"];
$offer["start_date"] = $row["Start_Date"];
$offer["end_date"] = $row["End_Date"];
$offer["o_desc"] = $row["O_Desc"];
$offer["short_desc"] = $row["Short_Desc"];
$offer["merchant_ID"] = $row["Merchant_ID"];
$offer["m_name"] = mysql_query("SELECT M_Name FROM MERCHANT WHERE MERCHANT_ID = '".$row["merchant_ID"]."'");
array_push($response["offers"], $offer);
}
$response["success"] = 1;
echo json_encode($response);
} else {
//no offer found
$response["success"] = 0;
$response["message"] = "No offer found";
echo json_encode($response);
}
?>
當我使用Web瀏覽器中運行這個PHP文件,我不能爲商家獲取所需的名稱,即使數據是存在於數據庫...它只會返回我「空」。
{"offers":[{"offer_id":"1","start_date":"2013-05-17","end_date":"2013-05-18","o_desc":"AAA","merchant_ID":"2","m_name":null}],"success":1}
我做了什麼錯誤或我是什麼人仍下落不明?請幫助..謝謝!
謝謝兄弟!你只是救了我^ _ ^我正在使用mysql_ *僅用於當前項目...將來不打算做任何開發我猜:X –
@JackyLau歡迎你 – Fabio