-1

Why do I getan error of Resource id #52?

I have this query:

$unit = $this->input->get_post('inCode');
$sql = mysql_query('select ID from type where code like "%'.$unit.'%";');

I just want to output the variable $sql in here:

$this->db->set('Unit', $sql);
$this->db->insert('structure');

I just get that error. I have to insert the ID from the table: type. It is an INTEGER and the column: Unit is also an INTEGER.

user987654321
  • 119
  • 1
  • 1
  • 9
  • 1
    Are you expecting `$sql` to contain the value of `ID`? It doesn't. It's a mysql result resource, from which you must fetch a row. `Resource id #52` is not an error, it is PHP attempting to give you a string representation of something which is neither a string nor any other kind of data structure, but rather a pointer to MySQL results via the database connection.. – Michael Berkowski Apr 01 '14 at 02:28
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Apr 01 '14 at 02:29
  • possible duplicate of [Mysql query with count Resource id #11](http://stackoverflow.com/questions/11255449/mysql-query-with-count-resource-id-11) – Michael Berkowski Apr 01 '14 at 02:30
  • Oh! Ahmmm.. Can I resolve this? It is my first time to encounter this problem? How can I convert the $sql value into INT? – user987654321 Apr 01 '14 at 02:32
  • What PHP framework are you using? It appears you have a method `$this->db->insert()` to insert data, but you are making a raw call to `mysql_query()`. If that is indeed a database `insert()` method, it follows that there is probably a framework method for performing a `SELECT` query rather than calling `mysql_query()` on your own. – Michael Berkowski Apr 01 '14 at 02:32
  • Look at the [`mysql_query()` documentation](http://us1.php.net/manual/en/function.mysql-query.php) There are several examples on how to call `mysql_fetch_assoc()` to retrieve a row, from which you would access the array key `$row['ID']` to get your value. – Michael Berkowski Apr 01 '14 at 02:33
  • @Michael Berkowski I'm using Codeigniter. I'm woking in Codeigniter model to insert data. – user987654321 Apr 01 '14 at 02:33
  • For Codeigniter, you should call `$this->db->query("SELECT.... ")`. Do not call `mysql_query()` manually. [It's all in the documentation](http://ellislab.com/codeigniter/user-guide/database/results.html) – Michael Berkowski Apr 01 '14 at 02:34
  • Thank you for the corretion. I just add: $row = $sql->row_array(); and output this: $this->db->set('Unit', $row['ID']); It works! – user987654321 Apr 01 '14 at 02:39

1 Answers1

0

Though I'm not sure what $this->db->set and $this->db->insert in your project were expect, but I guess you don't need to pass a resource (that was what mysql_query return)

Try using this instead

$sql = 'select ID from type where code like "%'.$unit.'%";';
Faiz Shukri
  • 95
  • 10