使用电子邮件地址选择

时间:2013-07-17 15:27:10

标签: php mysql sql

我正在使用PHP和PDO

尝试使用电子邮件作为密钥从表中检索记录。当电子邮件直接在sql语句中给出时,它可以正常工作,

但是当我在“$ bio_email”字段中发送电子邮件时,它不起作用。表中的电子邮件字段是“varchar”

类型

我尝试了几个选项,但两个都没有。系统返回的错误位于sql语句的右侧

这是我的代码

$bio_email      = $applicantinfo->user_email; // email from a table
$sql = "SELECT * FROM tb_files_upload WHERE ";
$sql .= " email = 'applicant1@live.ca' "; // THIS WORKS FINE
//$sql .= " email = 'danilo.gonzalez@parkinson.ca' "; // THIS WORKS FINE
//$sql .= " email = $bio_email "; // DOES NOT WORK  ... syntax to use near '@live.ca' at line 1
//$sql .= " email = {$bio_email} "; // DOES NOT WORK  ... syntax to use near '@live.ca' at line 1
//$sql .= " email = '{$bio_email}' "; // DOES NOT WORK ... syntax to use near ''applicant1@live.ca'' at line 1
//$sql .= " email = '$bio_email' "; // DOES NOT WORK ... syntax to use near ''applicant1@live.ca'' at line 1

$biosketchs = Tb_Files_Upload::find_by_sql($sql);

同样在同一张表中我有一个字段“dateReceived”。我想拥有2.5岁的记录。你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

我会尝试

$sql .= " email = '$bio_email' ";

表示dateReceived,只有DATE_SUB表示月份,而表示年份。

dateReceived >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 6 MONTH), INTERVAL 2 YEAR)

或者让我们不要愚蠢直接使用18个月

$sql.= " AND dateReceived >= DATE_SUB(CURDATE(), INTERVAL 18 MONTH)";