Jump to content

searching mysql with form variable


ntroycondo

Recommended Posts

I have a form passing user's search term, ie name.

$pname = mysql_real_escape_string($pname;

 

Not sure on proper syntax to include this in my select statement.

I also want it to return for partial string matched, like 'mik' for 'mike':

$result = mysql_query(
  "SELECT * FROM guest WHERE name like '$pname'"
);

Link to comment
https://forums.phpfreaks.com/topic/212443-searching-mysql-with-form-variable/
Share on other sites

Hi ntroycondo,

 

This is a MySQL question really and so is in the wrong forum.

 

What you need to use to get the desired result is the MySQL wildcard character (%), when this is used with the LIKE keyword, it is a wildcard for 0 or any number of characters.

 

E.g. if you wanted it to allow partial strings that all start with the correct letters you will want:

 $result = mysql_query(
  "SELECT * FROM guest WHERE name like '$pname%'"
);

 

Hope that helps!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.