search string form multiple column in database table - php

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%"
will treat all three fields like single field and result will be faster

Related

How to return the name of a column after mysql LIKE search

I am trying to do a google like search function on my customer database.
so when you type you get drop down suggestions with the word u searched for highlighted.
i have 2 tables:
customers and addresses (because some customers have multiple addresses)
here is the search query and it works perfectly:
$query = "
SELECT id FROM
customer
WHERE
name LIKE :search OR
surname LIKE :search OR
email LIKE :search OR
CONCAT(name, ' ', surname) LIKE :search
UNION
SELECT cust_id FROM
addresses
WHERE
CONCAT(address, ' ', town, ' ', postcode) LIKE :search
";
:search is obviously whats been searched for. It returns the customer id and from then i can display dropdown suggestions as they type. (done thru ajax)
What i am TRYING to achieve is rather than showing ALL the information(name surname email phone address town postcode etc) in the drop down suggestions, i would like to show The name and surname and whatever it is they are clearly searching for.
For example lets say on the database a customer is called:
Foo Bar.
tel: 0123456789
email: foo#bar.com
In the addresses table he has an address of 6 pack lane, petersville, EX23 6GH.
So as i type "pack" into the search field i want a drop down suggestion to come up with "Foo Bar, 6 Pack Lane"
But to do this i need to find out which column "Pack" refers to.
At the moment i am only returning the id because i dont know which piece of data is going to be searched for.
so is there a way of returning the name of the column that the LIKE query is returning?
many thanks

mysql query not working if middle text is missing

I am trying to search user based on full_name. I have database table with field full_name
Here are the records of table table : users
ID full_name
1 adam bell nithan jhon albrt
2 ali imam khan
If i use query like this then it will work,
select * from users where full_name like '%ali imam%'
select * from users where full_name like '%adam bell%'
But if i search like this it will not work,
select * from users where full_name like '%adam bell nithan albrt%'
select * from users where full_name like '%ali khan%'
I dont know why this happening even we used proper like query. Is there any solution regarding this issue?
You can try this:
select *
from users
where full_name like concat('%', replace('adam bell nithan albrt', ' ', '%'), '%');
It replaces the spaces with wildcards. However, you are still stuck with the ordering of the names.
I think you should look into full text search functions (see here). I think it might be exactly what you need, and the performance is much better than like.

MySQL Searching forename and surname

OK, I have a DB table that's called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example... in the input box, someone types James and there's a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
SELECT * FROM table WHERE match(combined_names) against('John Smith');
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
SELECT * FROM table WHERE match(combined_names) against('+John +Smith');
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
SELECT * FROM players WHERE CONCAT(forename, ' ', surname) = '$name' OR forename LIKE '%$name%' OR surname LIKE '%$name%'
split the name up on spaces so the query runs twice (if there is one space)
the query will run for both names
$nameBits = explode($name," ");
run the query for each piece of $nameBits
surname LIKE '%$nameBits[$i]%'

Displaying searched record for a query using 'like' at the top in mysql

I am writing a search query which makes a search based on first name and last name.
The query is like this:
select fname,lname
from users
where fname like fname like '%$name%' or lname like '%$name%';
if the user enters first name and last name in the search box,I have exploded it with a 'space' and the query goes like this
select fname,lname
from users
where fname like fname like '%$f_name%' or lname like '%$l_name%';
In the second query if I enter John Thomas,it shows me all the records with John or Thomas in first or last name,but the actual search result i.e John Thomas(exact match) is somewhere below in the results.(If I have 100 results its on the 60th position).
How can I modify the query to achive this or do I have to handle it programatically? i.e Check the result array and match the check box value for its presence and display it fist.
Try:
SELECT fname, lname FROM users
WHERE (fname = '$f_name' AND lname = '$l_name')
OR (fname LIKE '%$f_name%') OR (lname LIKE '%$l_name%');
And if you need to order then ORDER BY lname, fname in the end of the query, depending on what you need to order by of course.
ADD in query
ORDER BY `fname`
You can testing mysql sorting
More information here ==> http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

PHP: LIKE after space?

Right now i have a column called full_name where the first and last name gets stored. It is seperated by a normal space, so a name is stored like this: "Firstname Lastname". Now when im making this search users function, i have this:
$query = "SELECT full_name, id, user_name, sex, last_access, bostadsort FROM users WHERE full_name LIKE '$searchUser%'";
It works great by finding if you search by the firstname. I wish to make if you e.g type "lastname" then the user also will come up.
Now i find this quite my fault, that i started by having 1 column for the full_name and not firstname lastname columns.
So i wonder if i can do this without making two new columns and change rest of my code to work with firstname lastname new columns.. ?
Im using MySQL
full_name LIKE '$name%' OR full_name LIKE '% $name'
You should however split this column into two separate columns. What about multiple forenames/surnames?
My recommendation is to get them in 2 separate fields. Otherwise use % wildcard as well in the beginning of the string.
... WHERE full_name LIKE '$searchUser%' OR full_name LIKE '%$searchUser' OR full_name LIKE '%$searchUser%'
will that do?
Put a % in front of your variable to denote you want to match items at the end, a % in the back to denote you want to match the items in the front, and a % in front and back to denote the item you want to match is in the middle.

Categories