2010-12-01 141 views
1

我有一個特別令人費解的問題。使用電子郵件地址的PHP/MySQL SELECT查詢神祕

我正在使用PHP來遍歷一個記錄集,然後確定一個電子郵件地址是否存在於另一個表中。

代碼一切正常,直到它到達一個特定的電子郵件地址,我不能爲我的生活看到什麼是錯的。

電子郵件地址是[email protected]。我收到以下錯誤:

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 'ambro'' at line 1

其他所有郵件地址都沒有問題。

我贊同查詢

SELECT * FROM user_details WHERE email='[email protected]' 

,並在Navicat運行它和它的作品

PHP代碼如下:

if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 

/*Get source data*/ 
mysql_select_db($database, $link); 
$query_clients = "SELECT email FROM clients ORDER BY client_id DESC"; 
$clients = mysql_query($query_clients, $link) or die(mysql_error()); 
$row_clients = mysql_fetch_assoc($clients); 
$totalRows_clients = mysql_num_rows($clients); 

do { 
    /*Check table to see if email already exists*/ 
$query_check = sprintf("SELECT * FROM user_details WHERE email=%s",GetSQLValueString($row_clients['email'],"text")); 
echo "<br>".$query_check."<br>"; 
$check = mysql_query($query_check, $link) or die(mysql_error()); 
if (mysql_num_rows($check)==0) { 
    $query_insertUsers = sprintf("INSERT INTO users (username, password, userlevel) VALUES (%s, %s, 1)", $username, $password); 
    echo $query_insertUsers."<br>"; 
    //$insertUsers = mysql_query($query_insertUsers, $link) or die(mysql_error()); 
} 
} while ($row_clients = mysql_fetch_assoc($clients)); 

mysql_free_result($clients); 

正如我所說的 - 此代碼的工作,它只是當試圖用這個電子郵件地址查詢它失敗時。

+1

不使用回聲,嘗試`var_dump`和查看源代碼(如果在Web環境中) – ajreal 2010-12-01 14:19:20

+0

@Borealid - 有趣。我一定要看看。 – Pandy 2010-12-01 14:31:50

+0

更不用說你的GetSQLValueString函數是錯誤的 – 2010-12-01 15:16:33

回答

3

這看起來像逃跑出了問題:right syntax to use near 'ambro''似乎表明,電子郵件可能實際上是marcod'[email protected]。如果你

echo "<br>".$query_check."<br>"; 

,並在Navicat運行,是否具有同樣的錯誤?

+1

我同意。很有可能有一些UTF-8字符在查詢打印時沒有被呈現,但被MySQL解釋。 +1 – 2010-12-01 14:13:58

1

運行以下查詢:

SELECT * FROM user_details WHERE email LIKE 'marco%' 

我敢打賭,你真正擁有的數據庫是marcod'[email protected](注意是'含稅)。這可能發生在某些自動生成電子郵件地址的過程中。

0

你確定電子郵件是引號內?

相關問題