using search firstname and last name mixed php query - php

I have a SQL query command. The problem is when I type the whole name of a person it won't appear. For example if I search for "ermel", it will show ermel. However, when I search for "ermel lopez" it will fail to output the query. Here's my query:
$query=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' order by date desc
LIMIT $start,$per_page ");
I tried CONCAT, which works, but won't work on the executable:
SELECT *
FROM persons
WHERE CONCAT( firstname, ' ', lastname ) LIKE 'kaitlyn'
OR `LastName` LIKE 'pineda'
LIMIT 0 , 30

Try reversing the way you check your strings like this:
"select * from persons
where '$searchtext' like concat('%',firstname,'%')
or '$searchtext' like concat('%',lastname,'%')
order by date desc
LIMIT $start,$per_page "
This should result in a match on your searchtext containing either the firstname or lastname.

a LIKE without wildcards is an exact match!
you need
Like 'kaitlyn%'

I think your Sql Query is wrong
when you search Full name (ermel lopez)
Then from your query, you search it in both columns.
But whole text is not stored in a columns
Try this
$searchtext1="ermel";
$searchtext2= "lopez";
$query=mysql_query("select * from persons where firstname like '%$searchtext1%' or lastname like '%$searchtext2%' order by date desc
LIMIT $start,$per_page ");

Related

How to search data in combination of field

I have a student table with first_name and last_name. when i apply the search on first name or last name it give me the correct record but when i search with complete name it does not return any thing. Here is my query
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
((spi_first_name like '%test developer%') OR (spi_last_name like '%test developer%')
ORDER BY
`spi_first_name` ASC
Is there any way to make it searchable with both field combination.
It will give record in case we enter first name .middle name and last name but don't fetch in case of firstname and last name not middle name.
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE (
spi_scholar_no LIKE '%amir hussain khan%'
)
OR (
CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%amir hussain khan%'
I have also try this but its working for exact match only.
SELECT `student_parents_info`.`spri_spi_id` AS `sid`
FROM `student_parents_info`
WHERE (MATCH (spri_first_name, spri_middle_name, spri_last_name) AGAINST ('Pramod Kumar Sharma*' IN BOOLEAN MODE ) )
ORDER BY `spri_first_name` ASC
LIMIT 0 , 30
Please give any other suggestions.
Try searching using concat()..
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%test%'
ORDER BY `spi_first_name` ASC
It always works for me .
Try like this
SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE
((spi_first_name like '%amir khan%') OR (spi_last_name like '%test develope%'))
ORDER BY `spi_first_name` ASC
you are missing ')'
Split your test develope to test and develope using php, then write query like this :
$first = 'test';
$last = 'develope';
$query = "SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname` FROM `student_personal_info` WHERE
(spi_first_name like '%".$first."%') OR (spi_last_name like '%".$last."%')
ORDER BY `spi_first_name` ASC";
If you have more than two words, Split the words and create the dynamic where query.
Try with CONCAT:
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
CONCAT(spi_first_name, ' ', spi_last_name) LIKE '%test develope%'
ORDER BY
`spi_first_name` ASC
Do you receive a full name to perform the search with? or do you always get 2 separate (first and last name)?
If you get one full name string, change your query to concatenate first and last name, and use
GROUP BY HAVING(full_name LIKE %search_term%)
if you get two separate strings, just change your OR to AND between the first and last name LIKE tests.

Combine multiple column values in to single value in SQL select statement

I have an autocomplete field where user can enter lastname firstname middlename in the same order.
Using lastname firstname middlename (as a term) I have to show the autocomplete dropdown.
On my database I have 3 columns for firstname lastname and middlename. Now I have to compare 3 column values (asc_lastname, asc_firstname, asc_middlename) of same row with user input (term).
Here is the SQL query. Please correct my mistake.
$rs = mysql_query( "select asc_lastname, asc_firstname, asc_middlename from issio_asc_workers INNER JOIN issio_workers_asc_sc_list
ON issio_asc_workers.lname_fname_dob=issio_workers_asc_sc_list.lname_fname_dob where issio_workers_asc_sc_list.center_id='$cid'
AND issio_workers_asc_sc_list.center_user_type = '31' AND issio_workers_asc_sc_list.center_status <> 'deleted' AND
(issio_asc_workers.asc_lastname+' '+issio_asc_workers.asc_firstname+' '+issio_asc_workers.asc_middlename)
LIKE '". mysql_real_escape_string($term) ."%' ORDER BY issio_asc_workers.lname_fname_dob ASC LIMIT 0,10");
Is it possible to compare 3 column values at a time using a SQL query like
select *
from table
where (column1+''+column2+''column3) like 'this is good'
Sorry for my poor sentences.
Try this query:
SELECT * FROM your_table WHERE CONCAT(column1, ' ', column2, ' ', column3) LIKE '%this is good%'
Documentation for CONCAT

php mysql - search like - variable

I have a form where a user can type in the firstname to search, my query is not returning the correct results, what am I doing wrong?
$sfn = $_POST["Text1"];
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '$sfn'";
...
Maybe you should add %-signs to your keyword like this:
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'";
Your query will only return rows where firstname is equal to $_POST["Text1"]. When you use LIKE you can use a wildcard (%) to represent any number of characters.
This will find rows where firstname starts with $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '$sfn%'
This will find rows where firstname ends with $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn'
This will find rows where firstname contains $_POST["Text1"].
SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'
Note: Never use variables from $_POST without escaping them first. What if I searched for "O'Neil" (or worse "'; DROP TABLE ex_users; -- ")?
You should use %searchterm% - include the % wildcards.
$sql = "SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'";
It should be
"SELECT * FROM ex_usrs WHERE firstname LIKE '%$sfn%'"

distinct query with autosuggest

I'm trying to distinct a column for my autosuggest function. This is the query I have now:
$result=mysql_query("SELECT * FROM users WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%' ORDER BY firstname LIMIT 0, 10",$con) or die(mysql_error());
somehow just adding 'DISTINCT firstname' after select doesn't work. (Javascript gives an error.)
the * in the query is the trouble maker I guess, don't know exactly why..
Please assist with writing the right query! :)
Thanks in advance
You could use "group by firstname" in stead:
$result=mysql_query("
SELECT * FROM users
WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%'
GROUP BY firstname ASC
ORDER BY firstname
LIMIT 0, 10",$con)
or die(mysql_error());

SQL condition: (A=B AND C LIKE %D%) OR (A LIKE %B% AND C=D)

I've got a table of first names and last names.
I'm trying to make a jQuery instant search on an input, in order to find very quickly and precisely a person in a huge list of people.
When the first word is entered in the input, it might be the first name or the last name.
I do this : SELECT * FROM myTable WHERE firstName LIKE %content% OR lastName LIKE %content%
When two words are entered, it might be:
* the full firstname and a bit of the lastname
* the full lastname and a bit of the firstame
So I tried this query : SELECT * FROM myTable WHERE (firstName = content1 AND lastName LIKE %content2%) OR ( lastName = content1 AND firstName LIKE %content2%)
Unfortunately parenthesis seems to do nothing, and the query is not interpreted this way, I've got a lot of results, basically produced by the two LIKE %% condition
Anyone had deal with this before and could give me a hand?
Thanx
If one of the words is going to be the full first name of the full last name, while the other word is a partial of the other, why don't you split up the words first? Then you'd pass in two paramaters and have:
SELECT
*
FROM
myTable
WHERE
(
firstName = %content1%
AND lastName LIKE %content2%
) OR (
lastName = %content1%
AND firstName LIKE %content2%
)
I believe you are going to need to split the two words entered and use them indepenently in your query.
It would probably work this way (assuming that the two words are in the content variable):
SELECT ... WHERE
CONCAT(firstName, " ", lastName) LIKE content%
OR CONCAT(lastName, " ", firstName) LIKE content%
however this approach would not be very efficient (no index usage). I would split the two words into two variables (word1, word2) and make it something like:
SELECT ... WHERE
(firstName = word1 AND lastName LIKE word2%)
OR (lastName = word1 AND firstName LIKE word2%)
SELECT * FROM myTable WHERE
(firstName = 'content1' AND lastName LIKE content2%)
OR
(lastName = 'content1' AND firstName LIKE content2%)
When two words are entered, it might
be: * the full firstname and a bit of
the lastname * the full lastname and a
bit of the firstame
If it is ALL of (exact match) the first or last name, then the = test seems correct. Just in case it is an error in the PHP part, this is how it should look like.
$qry = '
SELECT * FROM myTable
WHERE (firstName = content1 AND lastName LIKE '%".mysql_real_escape_string(content2)."%')
OR ( lastName = content1 AND firstName LIKE '%".mysql_real_escape_string(content2)."%')';
If it is a bit of both, then you need the wildcard twice
$qry = '
SELECT * FROM myTable
WHERE (firstName LIKE '%".mysql_real_escape_string(content1)."%' AND lastName LIKE '%".mysql_real_escape_string(content2)."%')
OR ( lastName LIKE '%".mysql_real_escape_string(content1)."%' AND firstName LIKE '%".mysql_real_escape_string(content2)."%')';

Categories