user1641524
user1641524

Reputation: 15

How combine 3 mysql queries together?

I have 3 different types of queries to retrieve data in same page. How will I combine altogether? The following is the query:

<?php
$sql = "SELECT * FROM general WHERE ((day1sql >= now()))";
if($_GET!=""){
$mydate = mysql_real_escape_string($_GET['datepicker']);
if($mydate!=""){    
$sql = "SELECT * FROM general WHERE ((day1 = '$mydate'))";  
}       
}
if($_GET!=""){
$city1 = mysql_real_escape_string($_GET['cityText']);
if($city1!=""){ 
$sql = "SELECT * FROM general WHERE (city = $city1))";  
}       
}

datepicker is a calendar, users can get data according to date. And Have an dropdown menu, users can select city name. The first two queries is working fine. I am confused how to integrate the third query?

Upvotes: 0

Views: 106

Answers (2)

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

Something like this might work:

sql = "SELECT * FROM general WHERE (day1sql >= now())";

if(isset($_GET['datepicker'])){
    $mydate = mysql_real_escape_string($_GET['datepicker']);
    $sql .= " AND (day1 = '$mydate')";         
}
if(isset($_GET['cityText'])){
    $city1 = mysql_real_escape_string($_GET['cityText']);
    $sql .= " AND (city = $city1)";  
}       

// execute $sql

We are appending the sql with AND conditions. I believe that's what you are trying to do.

Upvotes: 0

eggyal
eggyal

Reputation: 126025

You can use the logical OR operator to combine your filter criteria:

SELECT * FROM general WHERE day1sql >= now() OR day1 = '$mydate' OR city = '$city1'

Upvotes: 2

Related Questions