How to do exact pattern match in query string - php

I have my website where I want to add search functionality for the user. My search criteria is First name and last Name with two different column in mysql databse.
eg: If user has entered xxx (First name) yyy (last Name) in search box then the result is like:
If exact match with this two string then only one result has to come
from database.
If doesn't match then simply contain in from database has to come. (eg
xx is present then it has to come)
I've used the following query:
Select * from mytable where FirstName like '%".$fname."%' or LastName like '%".$lname."%' order by DtLastModified desc
it is giving me all the result which matches this string.
eg:
xxx yyy
xx yy
x y
x
y
Can any one please help me on this.

this may work, replace your php variables for $fname $lname
SELECT * FROM mytable
WHERE CONCAT(firstname, '', lastname) LIKE '%".$fname.$lname."%'
OR (firstname LIKE '%".$fname ."%' OR lastname LIKE '%". $lname ."%')

For : if exact match with this two string then only one result has to come from database
replace your query from 'or' to 'and' lik below
Select * from mytable where FirstName like '%".$fname."%' and LastName like '%".$lname."%' order by DtLastModified desc

Related

Search a MySQL table for Comma Separated Area Codes

I'm trying to modify an existing script I have, that will only output rows that contain specific area codes to users that have be assigned the specific area codes.
Example:
A column named designed_areas in the users table will contain different data like: CM,SS,RH
When a lead comes in, I'm using substr to detect the first 2 characters of a post code.
$trimmed = substr("$postcode", 0, 2);
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' ORDER BY id DESC");
while($lead=mysqli_fetch_object($viewLeads)){
I'd like to expand on the mysqli_query and select all the leads from the database where the first 2 characters of the postcode matches any of users designated_areas.. which are comma seperated.
This way, he'll only see leads with the areas he can work on.
Can anyone help?
You can try to use a query like:
SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;
Php code:
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;");
You can do it with a regex. For example, in MySQL, if your postal_code table looks something like this:
PersonID Zips
1 11111,22222,33333
2 22222,12121,32323
And you want persons that have postal codes like 33*
SELECT PersonID FROM `postal_code` WHERE zips rlike '33[0-9]{3}'
This assumes that all postal codes are 5 digits and you're always searching using 2 digits. It basically says "find me 5 digit numbers where the first two digits are 33" or whatever your search numbers are.
The PHP code might look like:
$search = 33 ;
$sql = "SELECT PersonID FROM `postal_code` WHERE zips rlike '$search[0-9]{3}'" ;
This solution will find matches regardless of commas, spaces, etc. You won't need any further processing in your PHP code.

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

Comparing three fields

I have a table in which there are three fields
1. city
2. name
3. country
I am available with auto suggest in single Search field as comma seperated values for above given three fields, Now when I write anything on Text field of search and click search i must get all relevant resault. But my query seems to wrong as
I have writtent the WHERE clause as
SELECT * FROM mytable WHERE city LIKE '%$xyz%' OR name LIKE '%$xyz%' OR country LIKE '%$xyz%'
NOTE: while giving input in the search field I DO NOT SELECT ANY AUTOSUGGESTED VALUE
Please Help me to Rectify my query
You need single quotes in your LIKE clauses:
SELECT * FROM mytable WHERE city LIKE '%$xyz%' OR name LIKE '%$xyz%' OR country LIKE '%$xyz%'
SELECT *
FROM mytable
WHERE name like '%$name%' OR country like '%$country%' OR city like '%$city%'

Categories