I have a php page which needs to search multiple tables depending on what the user selects is that possible and if so am I on the right track with this statement?
<?php
switch($table_name){
case 'Table1':
$where_clause="Condition1=value1 AND condition2=value2 AND etc";
case 'Table2':
$where_clause="Condition1=value1 AND condition2=value2 AND etc";
...
etc.
}
$SQL="SELECT * FROM ".$table_name." WHERE ".$where_clause." ORDER BY ".$order_clause;
?>
thanks for your assistance ; but I am a bit unsure on how to use your given solution in respect of the condition part so I used your solution in the following way:
switch($tableSelect)
{
case 'companylist':
$where_clause="first";
}
//query table for information
$strSQL="SELECT * FROM $tableSelect WHERE $where_clause LIKE '%$text%' ORDER BY їContentId]";
$result= odbc_exec( $dbConnection, $strSQL);
It works fine but is there a way to add more columns to the $where_clause string; as it needs to look in multiple columns and if I try to add another column sperating the values with a comma I get an error .
By the way I didn't use the condition part b'cos the user will type the value in so it isn't predetermined and can range to any value the user types in.
To have multiple things in your WHERE clause, you need to use AND/OR. ex: SELECT * FROM table WHERE condition1=value1 AND (condition2=value2 OR condition3=value3) etc. Condition/value pairs are just the column and input values. If your table had a column called table_ID, then a where clause could be: WHERE table_ID=5 OR table_ID>15. Sorry for the ambiguous wording.
I would also suggest having the variables concatenated into the string, rather than being a part of it. Good code practice, and there are instances where including variables in a string can cause problems.
$strSQL="SELECT * FROM ".$tableSelect." WHERE ".$where_clause." LIKE '%".$text."%' ORDER BY [ContentId]";