0

I have a problem with the search data in the database whose value in the search of the array and string.

table (profile):

ID    Name status
1     Joe.     Free
2     Bill.      Not free

code:

$id = array('1', '2');
$query = $this->db->query("SELECT * FROM profile WHERE id = array and status='free' ");

I want check element of array and string (status) 1by1, if get one who does not fit then it will return false. how the trick ? thanks

1 Answers1

0

You cannot use the = operator to check for multiple values, you need to use the IN operator:

SELECT name FROM profile WHERE id IN (1,3)

You can use php's implode() function to create a comma separated list of an array's values.

In codeigniter you can use where_in() method:

$this->db->where_in();

Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate

$names = array('Frank', 'Todd', 'James'); $this->db->where_in('username', $names); // Produces: WHERE username IN ('Frank', 'Todd', 'James')

Shadow
  • 33,525
  • 10
  • 51
  • 64