2015-01-13 70 views
-1

我在php/mysql中遇到了我的腳本問題。這裏是由服務器顯示的錯誤:在php和mysql中的致命錯誤

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 'if exists (select * from notificacoes where uid in() order by id desc' at line 1' in C:\wamp\www\bigui\classes\Notificacoes.class.php on line 57 

這裏是我的PHP代碼:

static function listar(){ 

      $strIdAmigos = Amizade::$strIdAmigos; 

      $query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc'); 

      return $query->fetchAll(PDO::FETCH_ASSOC); 
     } 

我在mysql表是空的,沒有任何價值。當我插入一個值,錯誤消失,一切都很好。任何幫助?

+2

'$ strIdAmigos'的價值是什麼?它是如何計算的?因爲這個錯誤似乎是空的。 – Mureinik

+0

[由於在MySQL中使用保留字作爲表或列名而導致的語法錯誤]的可能重複(http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word -as-a-table-or-column-name-in-mysql) –

回答

1

如果$strIdAmigos爲空,則會導致語法錯誤。

在執行此查詢之前,您應該檢查$strIdAmigos值是否爲空以避免此問題。如果需要,不要忘記逃離價值觀。

+0

就像這樣$ strIdAmigos = Amizade :: $ strIdAmigos; \t \t \t $ query = self :: getConn() - > query('select * from notificacoes where uid in('。$ strIdAmigos。')order by id desc'); – user2701811

+0

wooah,什麼是'$ strIdAmigos = Amizade :: $ strIdAmigos;'?你可以通過編輯你的問題發佈你的代碼,而不是在評論中。 – Raptor

0

當您在變量$strIdAmigos中沒有任何內容運行查詢時,它將會出錯。

嘗試初始化和/或檢查你的變量$ strIdAmigos,運行您的查詢之前:

$strIdAmigos = ""; 

if (empty($strIdAmigos)) { 
    /* Uh oh, throw an error */ 
} else { 
    $query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc'); 
} 

注意,如果$strIdAmigos = "0",該empty($strIdAmigos)仍然會評估爲真,因此,將不會運行的查詢。