1

In my code i want to search the friends by its firstname , lastname and by a email fields. Now i want to match the exact string from the database table fields name.also i want to display that fields.also i want get the search data`s id so then after i will display the full description about that person.how can i do it. thanks any help for me. i try it.

$term=$_POST['find'];
$searchquery=mysql_query("select firstname,lastname,email from account where output LIKE %$term%");

4 Answers 4

2

You are looking for :

SELECT id, firstname, lastname, email
FROM account
WHERE firstname LIKE %$term%
OR lastname LIKE %$term%
OR email LIKE %$term%
LIMIT 20

Note that your way is potentially dangerous, you should protect your $term variable :

$term = mysql_real_escape_string($_POST['find']);

Anso note that mysql_* family function is under deprecation process, you should have a look to PDO or mysqli.

Edit:

Added a LIMIT because I don't think you want to recover all your database table if $term contains nothing.

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

6 Comments

ok bro its good but how can i display that fields and how to identified which fields is match from database.
i am display using this type is it give me error.pls check it
$resultsearch=mysql_query($searchquery); while($searchdata=mysql_fetch_array($resultsearch)) {?> <table border="0" cellpadding="0" cellspacing="0" align="center"> <tr> <td> <?php echo $searchdata['firstname']; echo $searchdata['lastname']; echo $searchdata['email']; ?> </td> </tr> </table> <?php } } ?>
Your mistake is here : <?php } } ?> : remove one brace }, you opened only one brace with while(){ and no more.
it is.. that's not as safe as a prepared statement but that's quite fine.
|
1

the query should be like this

$searchquery=mysql_query("select id,firstname,lastname,email from account where firstname LIKE '%$term%' OR lastname LIKE '%$term%' OR email LIKE '%$term%'");

if you want to search in either these 3 column(firstname,lastname,email).

Comments

1

Change your query to fetch all the needed info you want to display, and to check in your wherestatement against the term. Like this:

select id, firstname, lastname, email /*, ... other fields */ from account where firstname LIKE '%$term%' OR lastname LIKE '%$term%' OR email LIKE '%$term%'

Comments

1
select firstname,lastname,email from account where concat_ws(' ',firstname,lastname,email) LIKE %$term%"

will treat all three fields like single field and result will be faster

1 Comment

fistname barjo lastname hnick, looking for john will match? if that's really faster why not actually, using concat_ws perhaps. I'll test it thanks for the tip.

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.