CodeIgniter SQL构建

时间:2012-11-16 15:29:16

标签: sql codeigniter

我正在处理“发送我的密码”类型的功能,我的sql很糟糕,或者我的codeigniter很糟糕。表单验证正常,模型中的函数被调用,但没有任何内容返回。我必须检查提交的电子邮件地址的两个表,因为我们将用户和管理员帐户分成两个表。

控制器代码:

 public function send() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim');
    $this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />');

    if ($this->form_validation->run()) {
        // Check For A Matching Email
        $this->load->model('signin_model');
        if ($this->signin_model('email_verify')) {

            // There Was A Match
            echo "THERE WAS A MATCH";
        } else {

            // No Match
            echo "NO MATCH";
        }

    } else {

        // Form Did Not Validate
        $this->load->view('signin/forgot');
    }
}

型号代码:

public function email_verify() {
    $sql = "SELECT * FROM admin, staff WHERE staff_email = ? OR admin_email = ?";
    $email = $this->input->post('email');
    $query = $this->db->query($sql, $email, $email);

    if($query->num_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

错误:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM admin, staff WHERE staff_email = 'dsfsdf@test.com' OR admin_email = 

更新:我现在使用的修复查询是:

$sql = "SELECT admin_id, first_name FROM admin WHERE admin_email = ? UNION SELECT staff_id, first_name FROM staff WHERE staff_email = ?";

2 个答案:

答案 0 :(得分:1)

$this->signin_model不是一个功能。这是一个对象。要调用模型函数,可以将该函数作为对象的属性调用:

$this->signin_model->email_verify()

编辑$this->db->query的第二个参数应为数组。

$query = $this->db->query($sql, array($email, $email));

编辑2 :如果两个表之间没有链接,那么JOIN将无法帮助您。您可以尝试使用UNION

$sql = "SELECT * FROM staff WHERE staff_email = ? ".
    "UNION ALL SELECT * FROM admin WHERE admin_email = ?";

答案 1 :(得分:1)

 $query = $this->db->query($sql, array($email, $email));

试试吧。应该修复SQL错误