我有一個特別令人費解的問題。使用電子郵件地址的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);
正如我所說的 - 此代碼的工作,它只是當試圖用這個電子郵件地址查詢它失敗時。
不使用回聲,嘗試`var_dump`和查看源代碼(如果在Web環境中) – ajreal 2010-12-01 14:19:20
@Borealid - 有趣。我一定要看看。 – Pandy 2010-12-01 14:31:50
更不用說你的GetSQLValueString函數是錯誤的 – 2010-12-01 15:16:33