CodeIgniter Forums

Full Version: probleme with sql query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ahmedi[/eluser]
Hello, i want to make a query with the IN clause, in other words i want to get all data where ids belongs to an array. But have errors when executing the qury , any help ???
this is my code
Code:
$ids = array();
  for($i=0;$i<$number;$i++){
          if( $max != null)
             $ids[$i]= rand() % $max;
        }


$req1 = "SELECT tagid,tag,score FROM tags WHERE tagid IN $ids;

El Forum

[eluser]rogierb[/eluser]
You cannot use an array with id's
You have to specify it as a comma separated string.
Code:
$ids = "10,12,34,567453,23423,34"
$req1 = "SELECT tagid,tag,score FROM tags WHERE tagid IN  ( $ids )";

If you use AR you can specify an array
Code:
$this->db->where_in('tagid ', $ids);

El Forum

[eluser]umefarooq[/eluser]
just implode you id array with , as glue
Code:
$comma_ids = implode(',',$ids);

$this->db->where_in('tagid ', $comma_ids);

El Forum

[eluser]ahmedi[/eluser]
Ok thank you but how to formulate the query with this syntax
Code:
$db->where_in ??

El Forum

[eluser]rogierb[/eluser]
Basic AR:

Code:
$this->db->select('tagid,tag,score');
$this->db->where_in('tagid ', $ids);
$result_object = $this->db->get('tags')

This will produce the query: SELECT tagid,tag,score FROM tags WHERE tagid IN ( id,id,id etc )