2013-06-27 107 views
1

嘗試從mysql查詢中檢索一些信息,並打印出一個字符串,但無法理解問題,根據這裏http://php.net/manual/en/function.mysql-result.php它應該是正確的,但它返回空。mysql查詢結果到字符串返回nil

$sql = new mysqli('xxxx', 'xxxx', 'xxxxxx', 'xxxxx'); 
if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
exit; 
} 
echo " user id ", $user_id, "\n"; 
echo "event id ", $event_id, "\n"; 
$query ="SELECT A1.event_name,A1.start_date,A2.first_name,A2.last_name FROM OWN_EVENTS A1 INNER JOIN USERS A2 ON A1.event_id = $event_id WHERE A2.user_id=A1.user_id "; 
$result = $sql->query($query);  
if (!$result) { 
    sendResponse(417, json_encode("Query failed")); 
exit; 
} 
$row = mysql_fetch_row($result); 
echo "row[2] ", $row[2], "\n"; 
echo "row[3] ", $row[3], "\n"; 
//echo "result",$result,"\n"; 

$querySend ="SELECT email FROM USERS WHERE user_id = $user_id"; 
$resultSend = $sql->query($querySend);  
if (!resultSend) { 
    sendResponse(417, json_encode("Query failed")); 
exit; 
} 
$rowSend = mysql_fetch_row($resultSend); 
echo "rowSend[0] ", $rowSend[0], "\n"; 
echo $rowSend["email"]; 

LOG:

user id 8 
event id 95 

Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in /var/insertToNotifications.php on line 180 
row[2] 
row[3] 

Notice: Use of undefined constant resultSend - assumed 'resultSend' in /var/insertToNotifications.php on line 187 

Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in /var/insertToNotifications.php on line 191 
rowSend[0] 

結果在phpMyAdmin:

enter image description here

enter image description here

我的目標是能夠打印出類似這樣$body = "Hi,\n\n $row[2],$row[3] has invited you to $row[0] starting $row[1] \n\n \n\n ";

我該怎麼做?

回答

1

您正在將mysqli_ *函數與已棄用的mysql_ *函數進行混合。要繼續與mysqli的功能,讓您的結果,像這樣:

$query ="SELECT A1.event_name,A1.start_date,A2.first_name,A2.last_name FROM OWN_EVENTS A1 INNER JOIN USERS A2 ON A1.event_id = $event_id WHERE A2.user_id=A1.user_id "; 
$result = $sql->query($query);  
if (!$result) { 
    sendResponse(417, json_encode("Query failed")); 
exit; 
} 
$row = $result->fetch_row($result); 

這裏是關於方法的文檔,你可以在$result對象調用:http://www.php.net/manual/en/class.mysqli-result.php

+0

感謝您的答覆,已經遵循鏈接,必須錯過「我」部分 –

1

你混合的mysqli(請注意i)和mysql(注意iLACK)。這兩個庫是可互操作的,並且從一個平均值處理/結果是完全沒有意義的。

你可能想

$row = mysqli_fetch_row($result); 
      ^---- 

,並可能不應該反正混合庫的程序和麪向對象的版本。選擇一種風格並堅持下去。