Fiona
Fiona

Reputation: 29

to find a value within the array in mysql

i want to search data in an array .How can i search using mysql select command. i wrote the query as

$this->db->query("SELECT * FROM client_details WHERE dob IN ({implode(',', $data})");

$data is an array of dates .Please Help me for solving this..

Upvotes: 0

Views: 164

Answers (2)

Prerak Sola
Prerak Sola

Reputation: 10009

You can do something like this:

Option 1

$tmp = implode(',', $data);
$this->db->query("SELECT * FROM client_details WHERE dob IN ($tmp)");

Option 2

$this->db->query("SELECT * FROM client_details WHERE dob IN (".implode(',', $data).")");

Also, there is a mistake in your bracket formation.

It should be dob IN ({implode(',', $data)}"); [Curly braces should close after the closing parenthesis].

Upvotes: 0

Pankaj katiyar
Pankaj katiyar

Reputation: 464

try with

$data =array('0'=>'2015-01-23','1'=>'2015-01-22','2'=>'2015-01-21');
$tmp = implode('","', $data);
$tmp ='"'.$tmp.'"';
$sql= 'SELECT * FROM client_details WHERE dob IN ('.$tmp.')';
echo $sql;
$this->db->query($sql);

Upvotes: 2

Related Questions