2014-03-02 23 views
-2

我的MySQL查詢有問題,但是我看不到它告訴我的任何錯誤。我發展我的本地機器上,以下是數據庫連接語句:PDOException。 SQL語法錯誤

$db = new PDO('mysql:host=localhost;dbname=xsa_primary;charset=utf8', 'root', '',  array(PDO::ATTR_EMULATE_PREPARES => false, 
                         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); 

這裏是我的實際查詢頁面(對於預輸入搜索功能,typeahead.php):

$query = $_GET['query'].'%'; 

$stmt = $db->prepare("SELECT accountEmail FROM `xsa_accounts` WHERE accountEmail LIKE = :query"); 
$stmt->bindParam(':query', $query, PDO::PARAM_STR); 
$stmt->execute(); 

$results = array(); 
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $row) { 
    $results[] = $row; 
} 

return json_encode($results); 

我在該頁面上不斷收到以下錯誤,我嘗試了很多我的MySQL查詢的變體,包括反引號(`)以及每次一個特定的事情等。仍然沒有運氣。

的錯誤是:

致命錯誤:未捕獲的異常「PDOException」有消息「SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查與你的MySQL服務器版本相對應的手冊,在'=?'附近使用正確的語法。 (8):PDO-> prepare('1')in .......... \ typeahead.php:8 Stack trace:#0 .......... \ typeahead.php(8) SELECT accountE ...')#1 {main}在第8行拋出.......... \ typeahead.php

我的MySQL表有以下列(僅用於測試):

1 accountID PRIMARY AUTO_INCREMENT 
2 accountFirst 
3 accountLast 
4 accountEmail 
5 accountPhone 

我在這裏錯過了什麼?這讓我瘋狂。我一直盯着這個大約45分鐘。

+3

刪除刪除='=''後LIKE' –

回答

1

更換

$stmt = $db->prepare("SELECT accountEmail FROM `xsa_accounts` WHERE accountEmail LIKE = :query"); 

$stmt = $db->prepare("SELECT accountEmail FROM `xsa_accounts` WHERE accountEmail LIKE :query"); 

必須經過LIKE

+1

我覺得不輕鬆沒收白癡。謝謝! –

0

你不LIKE

LIKE = :query (bad) 
LIKE :query (good) 

明確地使用和等號:改變你的SQL語句來此

"SELECT accountEmail FROM `xsa_accounts` WHERE accountEmail LIKE :query"