HELP!!I Can't get UNION to work!!
In the below code, each query works correctly if I put it by itself, I get the right results. When I put them together with UNION, as below, I only get results from the second query.
I think its a syntax issue, and I can't find anything to show me the correct syntax in PHP. So I need to have a unique variable for each query joined by UNION? Do I call a $result after each query, or at the end of the UNION query? I'm getting nowhere on my own...
SOMEBODY HELP!!
code:--------------------------------------------------------------------------------<?
if (isset($_POST['Submit'])) { // Search database.
$message = NULL;
}
if ($POST['month']) {
$mo = $POST['month'];
}
if ($POST['year']) {
$y = $POST['year'];
}
if ($POST['salesperson']) {
$sp = $POST['salesperson'];
}
$query = "SELECT COUNT(deal_id)AS '# Sales' FROM salesdemo WHERE ";
if(isset($POST['month']) && ($POST['month']) !='00'){
$query .= "month = '$mo' AND ";
}
if(isset($POST['year']) && ($POST['year']) !='00'){
$query .= "year = '$y' AND ";
}
if(isset($POST['salesperson']) && ($POST['salesperson']) !='00'){
$query .= "salesperson = '$sp' AND ";
}
//minus the trailing 'AND '
$query=substr($query, 0, strlen($query)-4) ;
UNION;
$query = "SELECT COUNT(etch)AS '# Etch' FROM salesdemo WHERE etch !='0.00' AND ";
if(isset($POST['month']) && ($POST['month']) !='00'){
$query .= "month = '$mo' AND ";
}
if(isset($POST['year']) && ($POST['year']) !='00'){
$query .= "year = '$y' AND ";
}
if(isset($POST['salesperson']) && ($POST['salesperson']) !='00'){
$query .= "salesperson = '$sp' AND ";
}
//minus the trailing 'AND '
$query=substr($query, 0, strlen($query)-4) ;
$result=mysql_query($query)or die("Invalid query: " . mysql_error());