newbie help

From: Date: Sun, 25 Mar 2001 18:04:52 +0000
Subject: newbie help
Groups: php.db 
Request: Send a blank email to php-db+get-7778@lists.php.net to get a copy of this message
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 : m@threezero.co.uk :: m : 07747 845690 w : http://www.threezero.co.uk : : : t h r e e z e r o

« previous php.db (#7778) next »