Dynamic SQL Statements

Jeff Davies jeff_davies at byu.edu
Thu Jun 17 11:00:57 MDT 2004


Group,

Now that summer is on us I want to be more active in this group gleaning and 
posting info that has helped me and others.

I want to start out this little adventure with a question.

Does anyone know any tricks for creating dynamic sql statements from posted 
info.

For example:

select * from db where $sql[] (is from the array)";

I was thinking .. 

if ($_POST) { $and = "and"; }
						  
		foreach ($_POST as $key => $value){
		if ($value == true){
			$sql[] = ("AND ".$key." LIKE '%$value%' ");
		}

it only does the AND but can be easily made to do or's

So now that we have a array holding our sql statements from the post we plug 
the $sql[] into the sql statement after we implode it and combine it.

Does anyone have any ideas on how to improve this or ideas on what they have 
used to make a dynamic sql statement.

thanks
-Jeff



More information about the UPHPU mailing list