2010-10-28 108 views
2

我一直在嘗試使用下面的語句來查詢電子郵件地址,試圖逃避串成功我已經接受了失利小時後不過。PHP/MySQL錯誤查詢電郵地址

我使用的查詢是:SELECT id, email FROM user WHERE email = '$email'

這給了我一個錯誤:

MySQL 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 '@gmail.com' at line 1

我敢肯定,這很簡單..我只是似乎無法在任何地方找到答案的作品。

UPDATE#1

,我已被使用的代碼是:

$email = "[email protected]";

$sql = "SELECT id, email FROM user WHERE email = '$email'";

$result = mysql_query($sql) or die('Unable to connect: '.mysql_error());

更新#2

的電子郵件來自Facebook的連接API。

+1

請出示您正在使用的全部生成的查詢,以及PHP代碼 – 2010-10-28 11:57:02

+0

此外,哪裏是E-Mail地址從何而來?用戶輸入? – 2010-10-28 12:01:26

+0

您顯示的代碼正在生成語法錯誤('abc @ email.com'需要引號)。你能顯示你使用的確切代碼嗎?此外,請爲電子郵件地址執行'echo $ sql;'並顯示結果。 – 2010-10-28 12:05:44

回答

6

這可能是因爲你,你從Facebook獲得E-Mail地址連接包含引號,例如像

"Harry"@gmail.com 
"[email protected]" 

從Web服務(或其他地方)獲取數據時,你需要逃脫數據,以防止SQL injection和亂碼查詢像您的情況。

你的情況:

$email = mysql_real_escape_string($email);