Hi,
I have a query like below
$query = "SELECT id,event_id,dayNo,themeName,sessionTime,sessionTopic from agenda WHERE event_id='4' AND dayNo='2'";
I don't want to use the same query for all requests.How can i add the WHERE keyword to my query only if the required parameters are passed to my service. I can change query at run time.
Thanks in advance
WHERE keyword to my query
Moderator: General Moderators
Re: WHERE keyword to my query
you can write SQL query like this
may this will work for you
Code: Select all
$sql_add = '';
if (isset($eventid))
$sql_add = " AND event_id = '".$eventid."' ";
if (isset($dayno))
$sql_add = " AND dayNo = '".$dayno."' ";
$query = "SELECT id,event_id,dayNo,themeName,sessionTime,sessionTopic from agenda WHERE 1 = 1 $sql_add";
Re: WHERE keyword to my query
I personally use an array and implode() like
Code: Select all
$conditions = array();
if (isset($eventid))
$conditions[] = "event_id = {$eventid}";
if (isset($dayno)) {
$conditions[] = "dayNo = {$dayno}";
$query = "SELECT id,event_id,dayNo,themeName,sessionTime,sessionTopic from agenda";
if ($conditions)
$query .= " WHERE " . implode(" AND ", $conditions);