0

我正在使用带有 PDO 的 PHP

尝试使用电子邮件作为键从表中检索记录。当邮件直接在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 年前的记录。你能帮我解决这个问题吗?

4

1 回答 1

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)";
于 2013-07-17T15:29:34.543 回答