0

I am using the following code to select from a MySQL database with a Code Igniter webapp:

My Model

<?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;
}
}
?>

I have error like this

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

Most likely, this is a syntax error, but I just can't figure out which part of my code is responsible.

I've also gone through the Queries section of the user guide of CodeIgniter, but it wasn't explained clearly there.

Can anyone please tell me where my mistake is, and what is the correct syntax for what I'm trying to do?

5
  • At a guess - the second parameter of mysql_real_escape_string is the database connection. If that's a boolean, then your database connection is failing. I've no idea how you can go about debugging that in CodeIgniter, though Commented Oct 15, 2015 at 15:05
  • So, what should I do? please help me Commented Oct 15, 2015 at 15:11
  • You seem to have two versions of the same query in your code, one commented out, and one not. Does the commented-out version work? Commented Oct 15, 2015 at 15:20
  • If I run commented-out version i have error like this `` `` A PHP Error was encountered Severity: Warning Message: mysql_num_rows() expects parameter 1 to be resource, null given Filename: mysql/mysql_result.php Line Number: 63 Backtrace: File: C:\xampp\htdocs\simpeg\application\models\m_login.php Line: 29 Function: query 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 @andrewsi Commented Oct 15, 2015 at 15:29
  • but if I run another version on top i have error like this 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: 24 Function: where 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 Commented Oct 15, 2015 at 15:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.