【问题标题】:PHP / MySQL SELECT query using email address mysteryPHP / MySQL SELECT查询使用电子邮件地址之谜
【发布时间】: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函数错了

标签: php email mysql


【解决方案1】:

这看起来好像转义出了问题:right syntax to use near 'ambro'' 似乎表明电子邮件实际上可能是marcod'ambrosio@domain.com。如果你这样做了

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

在 Navicat 中运行 that,会不会有同样的错误?

【讨论】:

  • 我同意。很有可能有一些 UTF-8 字符在打印查询时没有被呈现,但正在被 MySQL 解释。 +1
  • 也许他的单引号是左单引号或右单引号,而不是常规单引号,Navicat 正在为他解决这个问题。
  • @Brad:根据我的经验,NC 不会为您修复错误,正是出于调试原因。某种零宽度字符可能......
【解决方案2】:

运行以下查询:

SELECT * FROM user_details WHERE email LIKE 'marco%'

我敢打赌,您在数据库中实际拥有的是marcod'ambrosio@domain.com(请注意包含的 ')。这可能发生在某种电子邮件地址的自动生成过程中。

【讨论】:

  • 不 - 绝对没有撇号或引号!
【解决方案3】:

您确定电子邮件在引号内吗?

【讨论】:

  • 是的。我正在使用 GetSQLValueString 函数(来自 Dreamweaver),它添加引号和转义字符。
猜你喜欢
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多