我有一个特别费解的问题。
我正在使用 PHP 遍历记录集,然后确定电子邮件地址是否存在于另一个表中。
代码在到达一个特定的电子邮件地址之前一切正常,但我终究无法看出哪里出了问题。
电子邮件地址是 marcodambrosio@domain.com。我收到以下错误:
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='marcodambrosio@domain.com'
然后在 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);
正如我所说 - 此代码有效,只有在尝试使用该电子邮件地址进行查询时才会失败。
最佳答案
这看起来好像转义出了问题:在 'ambro'' 附近使用正确的语法
似乎表明电子邮件实际上可能是 marcod'ambrosio@domain.com
。如果你这样做了
echo "<br>".$query_check."<br>";
在Navicat中运行那个,是不是同样的错误?
关于使用电子邮件地址之谜的 PHP/MySQL SELECT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4325032/