2014-02-18 151 views
0

我有一個查詢在那裏我指望行,但它顯示這個錯誤..致命錯誤:未捕獲的異常「PDOException」有消息「SQLSTATE [42000]:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from=? AND to=?' at line 1' in C:\wamp\www\network\profile.php on line 38

(!) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from=? AND to=?' at line 1 in C:\wamp\www\network\profile.php on line 38

這裏是我的代碼:

$sql_check_friend = "SELECT COUNT(*) FROM connection_request WHERE from=:me AND"; 
$sql_check_friend .= "to=:friend"; 

$sth = $db->prepare($sql_check_friend); 
$sth->bindParam(":me", $me); 
$sth->bindParam(":friend", $pageuserid); 
$sth->execute(); 

$count = $sth->fetchColumn(); 

if($count > 0){ 
echo "REQUEST SENT"; 
}else{ 
echo "NOT SENT"; 
} 

我無法弄清楚什麼是錯的..

+1

你在第一行缺少'';',在第二行缺少''''。 –

+0

我在這裏打破了代碼..在我的編輯器它是在一行..我將編輯這一個雖然.. – user3184462

回答

7

fromreserved word。它必須在查詢中轉義:

SELECT ... WHERE `from` := ... 
        ^-- ^--- 

而且同上,用於to以及 - 這是一個保留字了。

+0

@jorge:可能只是一個錯字在這裏,否則會有更多的SQL錯誤和PHP語法錯誤開機。 –

+0

謝謝..解決了這個問題..! :) – user3184462

相關問題