Re: [PHP] Another question - not exactly what i was looking for
how 'bout something like $query = 'select * from table_name where '; $and = ''; if ($lastname != '') { $query .= lastname = $lastname; $and = ' and '; } if ($firstname != '') { $query .= $comma.firstname = $firstname; $and = ' and '; } and so on on 1/15/02 1:53 PM, Phil Schwarzmann at [EMAIL PROTECTED] wrote: Yo, thanks for all your help. But it isn't exactly what im looking for. Let's say you had a database with the following four columns... -LastName -FirstName -Age -Weight ...and you wanted a page that would allow a user to search for one or more of these fields within the database. It would be easy if the user could only pick just one of the fields. But let's say that want to search only lastname and firstname, or maybe all four, or maybe just one. How could this be done? If I have code that looks like this... $query = select * from table where lastname='$lastname' and firstname='$firstname' and age='$age' and weight='$weight'; $result = mysql_query ($query); $num_results = mysql_num_rows($result); ...the $num_results is ALWAYS zero unless I typed in all four fields. Any help? Thanks! -- mike cullerton michaelc at cullerton dot com -- PHP General 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]
Re: [PHP] Another question - not exactly what i was looking for
construct your query like $query = select * from table where ; lastname='$lastname' and firstname='$firstname' and age='$age' and weight='$weight'; if (isset($lastname) and $lastname !=) then $query.= lastname=$lastname ; if (isset($firstname) and $firstname !=) then $query.= and firstname=$firstname ; and so on... You may need to do some more checking like if there already been a field selected earlier if so then add AND before the current field else don't add the AND . Hope that helps. - Original Message - From: Phil Schwarzmann [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Tuesday, January 15, 2002 3:53 PM Subject: [PHP] Another question - not exactly what i was looking for Yo, thanks for all your help. But it isn't exactly what im looking for. Let's say you had a database with the following four columns... -LastName -FirstName -Age -Weight ...and you wanted a page that would allow a user to search for one or more of these fields within the database. It would be easy if the user could only pick just one of the fields. But let's say that want to search only lastname and firstname, or maybe all four, or maybe just one. How could this be done? If I have code that looks like this... $query = select * from table where lastname='$lastname' and firstname='$firstname' and age='$age' and weight='$weight'; $result = mysql_query ($query); $num_results = mysql_num_rows($result); ...the $num_results is ALWAYS zero unless I typed in all four fields. Any help? Thanks! -- PHP General 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]
Re: [PHP] Another question - not exactly what i was looking for
$where_conditions = array(); if( !empty( $lastname ) ){ $where_conditions[] = lastname like '%${lastname}%'; } if( !empty( $firstname ) ){ $where_conditions[] = firstname like '%${firstname}%'; } if( !empty( $age ) ){ $where_conditions[] = age = ${age}; } if( !empty( $weight ) ){ $where_conditions[] = weight = ${lastname}; } $where_clause = ; $num_conditions = sizeof( $where_conditions ); if( $num_conditions 0 ){ $where_clause = where ; for( $c = 0; $c $num_conditions; $c++ ){ $where_clause .= $where_conditions[$c]; if( $c $num_conditions - 1 ){ $where_clause .= ; } } } $query = select * from table ${where_clause}; ... that should be flexible enough for you to add new fields to check, by only having to add column = $value values to the $where_conditions array. g.luck, ~Chris /\ \ / September 11, 2001 X We Are All New Yorkers / \ rm -rf /bin/laden On Tue, 15 Jan 2002, Phil Schwarzmann wrote: Yo, thanks for all your help. But it isn't exactly what im looking for. Let's say you had a database with the following four columns... -LastName -FirstName -Age -Weight ...and you wanted a page that would allow a user to search for one or more of these fields within the database. It would be easy if the user could only pick just one of the fields. But let's say that want to search only lastname and firstname, or maybe all four, or maybe just one. How could this be done? If I have code that looks like this... $query = select * from table where lastname='$lastname' and firstname='$firstname' and age='$age' and weight='$weight'; $result = mysql_query ($query); $num_results = mysql_num_rows($result); ...the $num_results is ALWAYS zero unless I typed in all four fields. Any help? Thanks! -- PHP General 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]