0

I have a mysql_query that must have WHERE columnName = '$variable1' in it. For example:

$result = mysql_query("SELECT * FROM database WHERE columnName = '$variable1'");

I want the contents of $variable1 to retrieve any value in ColumnName; sort of like a wildcard operator. I have tried using an asterisk (*) like this:

$variable1 = '*'
$result = mysql_query("SELECT * FROM database WHERE columnName = '$variable1'");

while($row = mysql_fetch_array($result)) {
    // Echo the content of ALL ROWS IN TABLE here
}

The result of this query, when echoed, should display all the rows in the database. The asterisk does not work as a universal variable, nor does a blank space (e.g. $variable = ''). What is the wildcard operator in MySQL that would match all text combinations?

1
  • if($var == '*') $result = mysql_query("SELECT * FROM database.table"); else $result = mysql_query("SELECT * FROM database.table WHERE columnName = '$variable1'"); what about this Commented Nov 27, 2013 at 2:42

3 Answers 3

3

the wildcard character in SQL is %. And you need to use LIKE instead of = for wildcard matching. For example:

SELECT * 
FROM table 
WHERE ColumnHas LIKE "%something%";

This example will match any string that has "something" in the middle, such as

"This is something to match". 

If you want wildcard match only at the beginning of the string, then exclude the trailing % (and vice versa).

For your case,

$variable1 = '%'
$result = mysql_query("SELECT * FROM database WHERE columnName LIKE '$variable1'");

should work.

But your use case makes me think no wildcard matching is necessary. You can also use

$result = mysql_query("SELECT * FROM database WHERE columnName NOT NULL");

provided columnName is defined to hold NULL as default value.

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to use two wildcards for two separate columns? For example: $result = mysql_query("SELECT * FROM database WHERE column1 LIKE % AND column2 LIKE %");
@JSW189 Yes, it is perfectly acceptable to use multiple wildcards in a single query.
@zsaat14 is right. You can use as many LIKEs as you like in a single WHERE clause. I just want to warn you that wildcard matching with LIKE can be BAD performance wise. It may result in table sweeps.
Please be careful scaling this up. Both the LIKE '%matchstring' and the NOT NULL matching instructions definitively defeat the use of indexes for searching.
1

This will work:

$variable1 = "' OR 1 = 1;"

Comments

0

If you want to retrieve rows with any value in a particular column, you simply refrain from mentioning that column in a WHERE clause.

2 Comments

That would be a simple solution, but the code needs the WHERE clause there. Is there any other way to work around this? Like a wildcard operator?
If you absolutely must have the where clause that does nothing, you can use WHERE 1 = 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.