title: Message: mysql_real_escape_string() expects parameter 2 to be resource, boolean given Filename: mysql/mysql_driver.php Line Number: 346
body: <p>I am using the following code to select from a MySQL database with a Code Igniter webapp:</p>
  
  <p>My Model</p>
  
  <pre><code>&lt;?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-&gt;security-&gt;xss_clean($this-&gt;input-&gt;post('username'));
      $password = $this-&gt;security-&gt;xss_clean($this-&gt;input-&gt;post('password'));
  
      $password = md5($password);
      // Run the query
      //$array = array('nip' =&gt; $this-&gt;db-&gt;escape_str($username), 'password' =&gt; $this-&gt;db-&gt;escape_str($password));
  
      //$this-&gt;db-&gt;select('pegawai.NIP, akun.PASSWORD, akun.ROLE');
      //$this-&gt;db-&gt;from('pegawai');
      //$this-&gt;db-&gt;join('akun', 'akun.ID_PEGAWAI = pegawai.ID_PEGAWAI');
      //$this-&gt;db-&gt;get_where('nip', $username);
      //$this-&gt;db-&gt;where($array);
      //$this-&gt;db-&gt;where('PASSWORD', 'd93591bdf7860e1e4ee2fca799911215');
      //$query = $this-&gt;db-&gt;get();        
  
      $sql = "SELECT NIP, PASSWORD, ROLE FROM pegawai LEFT JOIN akun ON pegawai.ID_PEGAWAI=akun.ID_PEGAWAI WHERE pegawai.NIP=('".$this-&gt;db-&gt;escape_str($username)."') AND akun.PASSWORD=('".$this-&gt;db-&gt;escape_str($password)."')";
      $this-&gt;db-&gt;query($sql);
      //$query = $this-&gt;db-&gt;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-&gt;db);
      //var_dump($username);
      if($query-&gt;num_rows == 1)
      {
           //If there is a user, then create session data
          $row = $query-&gt;row();
          $data = array(
                  'role' =&gt; $row-&gt;role,
                  'nip' =&gt; $row-&gt;nip,
                  'validated' =&gt; true
                  );
          $this-&gt;session-&gt;set_userdata($data);
          return true;
      }
       //If the previous process did not validate
       //then return false.
      return false;
  }
  }
  ?&gt;
  </code></pre>
  
  <p>I have error like this</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Most likely, this is a syntax error, but I just can't figure out which part of my code is responsible.</p>
  
  <p>I've also gone through the Queries section of the user guide of CodeIgniter, but it wasn't explained clearly there.</p>
  
  <p>Can anyone please tell me where my mistake is, and what is the correct syntax for what I'm trying to do?</p>