View Full Version : Can you use wildcards in php?

07-30-2008, 06:32 AM
In Rbase there was a command you could use to see if a text field contained a specified string. For example:

SELECT * FROM addresslist WHERE address2 CONTAINS "CA";

Is there a command like that in php? I know you can say in phpMyAdmin:

SELECT * FROM addresslist WHERE address2 LIKE "%CA%";

but I get the feeling you can't use wildcards in straight php. Can you?


07-30-2008, 06:37 AM
You can use wildcard characters in SQL statement in PHP or rather in any language. From PHP's point of view it is just a string nothing else. But this string will be get executed within a database and against a table and at that point this string will be considered as a valid SQL statement. In a valid SQL statement you can use wildcard characters.

07-30-2008, 06:42 AM
So this is a valid statement?

$sql = 'SELECT * FROM addresslist WHERE address2 LIKE "%CA%" ';

07-30-2008, 07:28 AM
Yes the SQL statements are valid.

07-30-2008, 09:18 AM
Yes, but in the second question, are the underscores wildcards? And if not, how could this query ever find any data to match the where clause? But it does, which makes no sense to me. That is the question.

07-30-2008, 10:05 AM
I am extremely sorry Kuau in MySQL underscore character is wildcard character for representing a single character.

$day = "_y_____";

If you consider the above one it will check all the value whose second character is 'y' and the total length of the value is 7.