【发布时间】:2010-12-01 13:53:32
【问题描述】:
我有一个特别令人费解的问题。
我正在使用 PHP 循环遍历记录集,然后确定电子邮件地址是否存在于另一个表中。
代码一切正常,直到它到达一个特定的电子邮件地址,我终其一生都看不出哪里出了问题。
电子邮件地址是 marcodambrosio@domain.com。我收到以下错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“ambro”附近使用正确的语法
所有其他电子邮件地址都可以。
我回显查询
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);
正如我所说 - 此代码有效,只有在尝试使用此电子邮件地址查询时才会失败。
【问题讨论】:
-
不要使用 echo,试试
var_dump并查看源代码(如果在 web 环境中) -
@Borealid - 很有趣。我一定要调查一下。
-
别说你的GetSQLValueString函数错了