简体   繁体   中英

search string form multiple column in database table

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%");

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.

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).

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%'
select firstname,lastname,email from account where concat_ws(' ',firstname,lastname,email) LIKE %$term%"

将所有三个字段都视为单个字段,结果将更快

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM