mercredi 20 juillet 2016

How to use if else in mysql where clause

I want to query data in mysql like:

$cid=$_POST["cid"];
$room_id=$_POST["room_id"];
$time=$_POST["time"];

$reuslt=$this->db-query("SELECT * FROM mytable WHERE if($cid is not null){then check cid=$cid} else{don't check} AND if($room_id is not null){then check room_id=$room_id} else{don't check} AND if($time is not null){then check time=$time} else{don't check}");

How could I writ the correct sql to query data?

Aucun commentaire:

Enregistrer un commentaire