Take the 2-minute tour ×

I know how to perform mysql searches using for example the WHERE word. But my problem is that i need to search on different values, but these can vary in number. For example:

I can search for 3 variables Name, LastName, Age BUT I in other search, i can look for 2 variables Name, Age.

Is there a way to perform a MYSQL search with the same script, no matter the quantity of values i search.??

Ot it is a better practice to "force" the search of a fixed amount of variables.??



share|improve this question

3 Answers 3

IMHO, it is far better to limit the search to a fixed number of variables. That way you are answering a specific question for a specific reason, not trying to fit a general answer to your specific question. Limiting the search criteria makes the statement(s) easier to debug and benchmark for performance.

Hope this helps.

share|improve this answer
What mean IMHO.? –  Roberto de Nobrega May 3 '12 at 22:29
In My Humble Opinion –  geoB May 4 '12 at 3:24

Just use a variable for your search parameters and inject that into your query. Just ensure that in the function/method you put the variable into the proper format (which will depend on how you select the different values.)


There will be no WHERE clause seen, unless it is passed your values (meaning you can use this same query for a general search of the db) without fear of having an empty/required $variable.

Your $variable when constructed would need to have to have the WHERE clause in it, then each value you add, insert it (in a loop perhaps) in the proper format.

Hope this makes sense, if not let me know and I will try to clarify. This is the same method most people use when paginating (except they put the variable in the LIMIT instead of the WHERE)


Also make sure to properly sanitize your variable before injection.

share|improve this answer
Yes, I understand... Nice explanation.! I'll try this.. Thanks.! –  Roberto de Nobrega May 3 '12 at 22:27

Simple example of dynamically building a query:

$conditions = array();

if (...) {
    $conditions['age'] = $age;
if (...) {
    $conditions['name'] = $name;

if (!$conditions) {
    die('No conditions supplied');

// if you're still using the mysql_ functions and haven't done so before:
$conditions = array_map('mysql_real_escape_string', $conditions);

foreach ($conditions as $field => &$value) {
    $value = "`$field` = '$value'";

$query = 'SELECT ... WHERE ' . join(' AND ', $conditions);

It's really not hard to dynamically cobble together the exact query you want to create. Just be careful you don't mess up the SQL syntax or open yourself to more injection vulnerabilities. You may want to look at database abstraction layers, which pretty much allow you to pass a $conditions array into a function which will construct the actual query from it, more or less the way it's done above.

share|improve this answer
This piece of code looks interesting... I am going to try as you wroted it.! Thank you very much.! –  Roberto de Nobrega May 3 '12 at 22:30

Your Answer


