Search system not searching both name and surname - php

I have a search system which helps you filter people list
My Table (People)
id | name | surname | email
There is a textbox which you can enter the name, surname or email so the list is filtered as you type.
It finds the records if I write only name or only surname. But if I enter name and surname it does show nothing.
I have tried Mysql's CONCAT function. But it did not work.
My query:
SELECT *
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
I have tried this
SELECT *
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
OR CONCAT(name, char(32), surname) LIKE :p4
and this
SELECT id,
name,
surname,
CONCAT(name, char(32), surname) AS fullname
FROM People
WHERE name LIKE :p1
OR surname LIKE :p2
OR email LIKE :p3
OR fullname LIKE :p4
How can I obtain the system that can find results when name + [space] + surname is entered?

You can split the keywords and search for each individual keyword (or expression) in your columns (name, surname and email).

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

Show full name from two columns in mysql table

I am selecting a full name from two columns in mysql table but the input takes only one order to return the information.
firstname | lastname
Amaj | Ato
With this data I want to select the full name (Amaj Ato or Ato Amaj) from one text box when the user enters Amaj Ato or vice versa (Ato Amaj).
This is what I have tried so far
`SELECT * FROM `table_name` WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'; `
This is able to select the firstname and lastname in only one order. For instance if the user enters Amaj Ato the query gives the full name but when the user enters the firstname after the lastname ( like Ato Amaj), the query returns empty. I want the user to get the full name in any order he enters the names. Thanks for helping.
You need to "tell" the query what you are looking for, it doesn't magically know that names could be switched. To do this, you can simply add an OR specifying the other valid case:
SELECT * FROM `table_name`
WHERE concat_ws(' ', 'firstname', 'lastname') like '%$fullname%'
OR concat_ws(' ', 'lastname', 'firstname') like '%$fullname%'

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

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

Categories