1

I am trying to have PHP search based on letters entered into a text box. I want it to match a pattern and find the relevant results from the database.

So if I typed in

"Jo Smi"

It would find results

Jo Smith, Pojo Smithson, Ojo Smith

How would one achieve this. I know how to get information from the database but not parts of various fields.

The objective is to search through users searching in fields user_firstname, user_lastname and user_email

Any ideas?

1
  • All of these are correct answers: 'like and a wild card', however, simply dropping unvetted text into an SQL statement is potentially a security risk (watcher hints at this with $escapedTextString. Read up a little on SQL injection attacks. Commented Apr 21, 2011 at 17:10

4 Answers 4

1

You should be able to do that directly from your query.

get the text box results and escape them then do

SELECT * FROM `database` WHERE `name` REGEXP '{INPUT TEXT HERE}';

That should work I think.

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

1 Comment

LIKE will be faster but you can get more exact results from REGEXP. Depends on your need
0

You can do simplistic searching on fields in a database using the LIKE clause:

$query = "SELECT Fields FROM Table WHERE TestField LIKE '%".$escapedTextString."%'";

Comments

0

Do a LIKE search against an upper-cased version of your search terms. Don't forget to mysql_real_escape_string().

$searchpattern = mysql_real_escape_string(strtoupper("Jo Smi"));
mysql_query("SELECT * FROM table WHERE UPPER(column) LIKE '%$searchpattern%';"); 

Comments

0

Hello Man previous answer is good but you want to know this info

  1. get the variable => $name = $_GET['name'];

  2. if you want to make it upper case use strtoupper();

  3. if you want to make it Lower case use strtolower();
  4. if you want to search you should replace = with where like where name = $name to where name like $name
  5. this info
  6. abc% => variable start with abc and finish with any thing
  7. %abc% => variable start any thing , contain abc , finish with any thing
  8. %abc => variable start any thing ,finish with abc

Comments

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.