Welcome Guest, Not a member yet? Register   Sign In
mysql_real_escape_string() error when loading query in model
#1

[eluser]SouthOfZeeland[/eluser]
I have a strange problem. Consider this model and controller. (note: I autoload the database library).
Code:
class TestModel extends Model {

  private $created_on;

  function __construct() {
    parent::Model();
    $this->created_on = time();
  }

  function dbTest($value) {
    $sql = "select * from sometable where name = ?";
    $query = $this->db->query($sql, $value);
    return $query->num_rows();
  }

}
Code:
class TestController extends Controller {

  function TestController() {
    $this->load->model('TestModel');
    $testvar = new TestModel;
    echo $testvar->dbTest('John');
  }

}

It should output 0, but instead I get this error:
Code:
A PHP Error was encountered

Severity: Warning

Message: mysql_real_escape_string() expects parameter 2 to be resource, integer given

Filename: mysql/mysql_driver.php

Line Number: 227
When I use this code in my model:
Code:
function dbTest($value) {
  $sql = "select * from sometable where name = '$value'";
  $query = $this->db->query($sql);
  return $query->num_rows();
}
...it works just fine. So the problem lies within the query bindings.

Any ideas?

TIA
#2

[eluser]gtech[/eluser]
hey I copied you code exactly and it worked.. odd, dunno if that helps

Code:
class Testmodel extends Model {

  private $created_on;

  function __construct() {
    parent::Model();
    $this->created_on = time();
  }

  function dbTest($value) {
    //modified this bit so it worked on my DB
    $sql = "select * from users where name = ?";
    $query = $this->db->query($sql, $value);
    return $query->num_rows();
  }

}

Code:
class Test2 extends Controller {
  function test() {
    $this->load->model('Testmodel');
    $testvar = new Testmodel;
    echo $testvar->dbTest('John');
  }
}

http://...index.php/test2/test
Result:


0
#3

[eluser]SouthOfZeeland[/eluser]
As you might expect: it doesn't work...
#4

[eluser]gtech[/eluser]
not sure why it works on mine and not on yours though... it must be because our set up differs somehow (I do not receive the error message you are getting at all), I will have a hunt around for you but im a bit stumped
#5

[eluser]gtech[/eluser]
I might have an idea.... the clue is in the error message

in your file system\database\drivers\mysql\mysql_driver.php

what line of code is at line 227 on your setup?

mine is this
Code:
function escape_str($str)    
    {    
        if (get_magic_quotes_gpc())
        {
            return $str;   // <------ GTECH... THIS IS MY LINE 227
        }

        if (function_exists('mysql_real_escape_string'))
        {
            return mysql_real_escape_string($str, $this->conn_id);
        }
        elseif (function_exists('mysql_escape_string'))
        {
            return mysql_escape_string($str);
        }
        else
        {
            return addslashes($str);
        }
    }

Now whats odd is I put an echo before my line 227 ran the code above, and the echo was displayed.. This means on my set up it does not even call mysql_real_escape_string, because get_magic_quotes_gpc() condition is met.

On yours it seems as if the if condition is not true, and then calls mysql_real_escape_string, which throws an error.... what is also odd is that its saying that mysql_real_escape_string is on line 227 in your errormessage... however mine is return $str.... which suggests our driver files differ slightly!

what does your line 227 looklike?

aaaand

what does the escape_str function look like?




Theme © iAndrew 2016 - Forum software by © MyBB