我使用以下代码从带有Code Igniter webapp的MySQL数据库中进行选择:
我的模特
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class m_login extends CI_Model{
function __construct(){
parent::__construct();
}
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$password = md5($password);
// Run the query
//$array = array('nip' => $this->db->escape_str($username), 'password' => $this->db->escape_str($password));
//$this->db->select('pegawai.NIP, akun.PASSWORD, akun.ROLE');
//$this->db->from('pegawai');
//$this->db->join('akun', 'akun.ID_PEGAWAI = pegawai.ID_PEGAWAI');
//$this->db->get_where('nip', $username);
//$this->db->where($array);
//$this->db->where('PASSWORD', 'd93591bdf7860e1e4ee2fca799911215');
//$query = $this->db->get();
$sql = "SELECT NIP, PASSWORD, ROLE FROM pegawai LEFT JOIN akun ON pegawai.ID_PEGAWAI=akun.ID_PEGAWAI WHERE pegawai.NIP=('".$this->db->escape_str($username)."') AND akun.PASSWORD=('".$this->db->escape_str($password)."')";
$this->db->query($sql);
//$query = $this->db->query("SELECT NIP, PASSWORD, ROLE FROM pegawai LEFT JOIN akun ON pegawai.ID_PEGAWAI=akun.ID_PEGAWAI WHERE pegawai.NIP='$username' AND akun.PASSWORD='$password'");
// Let's check if there are any results
//var_dump($this->db);
//var_dump($username);
if($query->num_rows == 1)
{
//If there is a user, then create session data
$row = $query->row();
$data = array(
'role' => $row->role,
'nip' => $row->nip,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
//If the previous process did not validate
//then return false.
return false;
}
}
?>
我有这样的错误
A PHP Error was encountered
Severity: Warning
Message: mysql_real_escape_string() expects parameter 2 to be resource, boolean given
Filename: mysql/mysql_driver.php
Line Number: 346
Backtrace:
File: C:\xampp\htdocs\simpeg\application\models\m_login.php
Line: 27
Function: escape_str
File: C:\xampp\htdocs\simpeg\application\controllers\login.php
Line: 26
Function: validate
File: C:\xampp\htdocs\simpeg\index.php
Line: 292
Function: require_once
最有可能的是,这是一个语法错误,但我无法弄清楚我的代码的哪一部分负责。
我还浏览了CodeIgniter用户指南的Queries部分,但在那里没有明确解释。
任何人都可以告诉我我的错误在哪里,以及我正在尝试做什么的正确语法?