faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 31 people (58%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How to search with wildcards from PHP form? MySQL wants %$var% but this doesn't work.

Apr 23rd, 2001 15:37
Shannon Hale, Wyatt James,


I've been able to get this to work by concatenating a string as follows 
(assumes that we are getting text from an input called "field"):
...
// Check whether the field was blank, and if not, 
// create a where clause in $limit.
if ($field != "") {
     $limit = "where myName like '%" . $field ."%'";
}
// Create your query string...
$query = "select myName from myTable " . $limit;
// Connect to the database - snipped...
// This should work...
$records = mysql_query($query)
...
So if the user typed "blah" in the text field, $limit will be "where 
myName like '%blah%'".