MySQL Searching forename and surname - php

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]%'

Related

Select results that contains part of string mysql php

I'm looking for a method to get rows that are more similar with inserted string
For example:
SELECT * FROM db WHERE column have part of '$search_string';
In other words if search string is My name is TOM and in the column I have
COLUMN
---------------
My name is Sara
My name is Jack
My dog is white
---------------
results must be:
all rows that contains My name is, because they are most similar to $search_string.
I've tried to use LIKE operator with phrase splatted in words, but I'm not obtaining the result that I want, any ideas?
What about
SELECT * FROM db WHERE COLUMN LIKE 'My name is %'
?
UPDATED
"SELECT * FROM db WHERE COLUMN LIKE '%$search%'"

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.

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%'

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