2012-12-20 47 views
0

這表明我:PHP錯誤

Error: 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 'WHERE company='ABC' AND branch='26' AND owner IS NULL' at line 1 

$sql="SELECT * FROM spr ORDER BY id WHERE company='$_SESSION[company]' AND branch='$_SESSION[branch]' AND owner IS NULL"; 

我看不出有什麼與我的查詢會錯。 Someboby幫助請...

+1

請了解使用參數化查詢,最好使用PDO。您的代碼使用不受信任的數據來構建可執行的SQL代碼,這會讓您打開SQL注入。 http://bobby-tables.com/php.html向您展示了多種在PHP中執行參數化查詢的方法。 –

回答

5

order by子句必須在where子句之後出現。

1

試試這個,這是ORDER BY條款,應在聲明的結尾

$sql="SELECT * FROM spr 
     WHERE company='".$_SESSION[company]."' 
     AND branch='".$_SESSION[branch]."' 
     AND owner IS NULL ORDER BY id"; 
0
$sql="SELECT * FROM spr WHERE company='$_SESSION[company]' AND branch='$_SESSION[branch]' AND owner IS NULL ORDER BY id"; 
0

你需要花括號{}各地數組值和對象屬性在PHP中使用了雙引號。

$sql="SELECT * FROM spr 
     WHERE company='{$_SESSION[company]}' 
     AND branch='{$_SESSION[branch]}' AND owner IS NULL 
     ORDER BY id "; 

而且你ORDER BY條款需要來結尾。