Hi all

Having created a form that sends values to a php file that searches a table
and returned results in the format specified I was on a high (little things
I know!). 

However, this only works if I enter values into all fields on the form. How
can I create an SQL query (mySQL) that will use only variables that have
values entered in the previous form and ignore those fields that were not
filled in?

At the moment I am using a simple query like (this query is a shortened
version for this email):

$sql = "SELECT ref, firstname, surname FROM artistes WHERE sex = '$sex' AND
heightFt = '$heightFt' AND heightIn= '$heightIn' ORDER BY surname";

$result=@mysql_query($sql, $connection) or die ("bugger...!");

---

I have been trying to detect if the variable holds any value and then
building a query by concatenating like this:

// ASSEMBLE SQL QUERY
// make the select
$select = "SELECT ref, firstname, surname ";

//add the tablename to the query
$fromtable = "FROM ";
$fromtable .= $tablename;

//build where clause
$where = " WHERE ";

//determine what we are selecting
if (!$sex) {
$Qsex = "sex = '$sex' ";
} else {
$Qsex = "";
}

if (!$heightFt) {
$QheightFt = "AND heightFt = '$heightFt' ";
} else {
$QheightFt = "";
}

// specify AND OR
$Qconcat = "AND ";

// specify how results are ordered
$orderby = "ORDER BY surname";

$concatsql = $select;
$concatsql .= $fromtable;
$concatsql .= $where;
$concatsql .= $Qsex;
$concatsql .= $Qconcat;
$concatsql .= $QheightFt;
$concatsql .= $orderby;

$result=@mysql_query($concatsql, $connection) or die ("bugger...!");

This just dies on mysql_query.

Sorry to make this such a long post, but this is really getting to me and I
believe there must be a simple answer that I am missing.

Can anyone help... please?!!

cheers
matt

t  h  r  e  e  z  e  r  o     :       :      :

the mill, millstone lane, leicester, le1 5jn
e : [EMAIL PROTECTED] ::  m : 07747 845690
w : http://www.threezero.co.uk

:       :      :    t  h  r  e  e  z  e  r  o



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to