|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
dynamically generate mysql queries
Hi all, I have a form which submits 5 variables to the script below. I would like to build some sort of array or way to catch all the variables sent as null or blank (the user left a field blank) and not include those variables in the mysql query. I have been messing with the idea of building an array of the input variables and using foreach to eliminate the null values, however it doesnt seem to be working.
ANY HELP AT ALL GREATLY APPRICATED, im stumped and going crazy =) $query = ''; foreach($articlename && $articlenum && $date && $partnum && $model as $key => $value) { if(!empty($input[$key])) { $query .= "AND $key = $value"; } } mysql_connect (host,user,pass); $result = mysql_db_query ("db","SELECT * FROM articles WHERE $query"); |
|
#2
|
|||
|
|||
|
RE: dynamically generate mysql queries
Your building your query like this:
SELECT * FROM articles WHERE AND ... HTH. |
|
#3
|
|||||||
|
|||||||
|
RE: dynamically generate mysql queries
Quote:
This won't work I think. You appear to be bitwise ANDing the values together. I think what you want is: php Code:
Cheers, Keith. Quote:
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > dynamically generate mysql queries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|