PHP如何编码(转义)@,用于电子邮件验证?

PHP如何编码(转义)@,用于电子邮件验证?,php,escaping,Php,Escaping,我得到了这个错误: 错误号码:1064 您的SQL语法有错误;在第1行“@gmail.com”附近,查看与MySQL服务器版本对应的手册,以获取正确的语法 代码如下: $email = $this->input->post('email'); $checkEmail = $this->crud_model->retrieve_where('employee', 'email', 'email', $email); 积垢模型: funct

我得到了这个错误:

错误号码:1064 您的SQL语法有错误;在第1行“@gmail.com”附近,查看与MySQL服务器版本对应的手册,以获取正确的语法

代码如下:

$email = $this->input->post('email');

                $checkEmail = $this->crud_model->retrieve_where('employee', 'email', 'email', $email);
积垢模型:

function retrieve_where($table, $table_id, $table_name, $value) {
        $table = $this->db->query('Select * FROM ' . $table . ' Where ' . $table_id . ' = ' . $value);
        $records = array();
        foreach ($table->result() as $row) {
            $records[] = $row->$table_name;
        }
        return $records;
    }

您尝试执行的查询是:

从电子邮件中选择*,其中电子邮件=email@email.com

在本例中,电子邮件是一个字符串,需要引用字符串

function retrieve_where($table, $table_id, $table_name, $value) {
    $table = $this->db->query("Select * FROM " . mysql_real_escape_string($table) . " Where " . mysql_real_escape_string($table_id) . " = '" . mysql_real_escape_string($value) ."'");
    $records = array();
    foreach ($table->result() as $row) {
        $records[] = $row->$table_name;
    }
    return $records;
}

别忘了用
mysql\u real\u escape\u string()
对查询进行转义。它会保护你不被注射

基本上使用“”将您的值括起来,选择*FROM employeeb@gmail.com'引用字符串:
Select*FROM employee Where email='1!'b@gmail.com“
您没有显示retieve_where()的功能,但我要确认它没有使用预先准备好的语句,或者正确地引用字符串引号引号引到哪里$这->积垢模型->检索“哪里('employee'、'email'、'email'、$email”);发布正在构建queryDon的crud_模型,不要通过将字符串混合在一起来构建SQL。同时使用绑定参数。。如果这是一个泛型函数,那么如果该值是一个数字,它将不起作用。@lemil77 MSSQL和MySQL正在为您转换它。但是你应该考虑另一种方法来执行你的查询。@你的常识可以证明你的评论吗?