Welcome Guest, Not a member yet? Register   Sign In
probleme with sql query
#1

[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;
#2

[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);
#3

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

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

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

[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 )




Theme © iAndrew 2016 - Forum software by © MyBB