How to search data in combination of field - php

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.

Related

Codeigniter db->where_in adding ''

I am having a problem with where_in . I am trying to get the shop name which possess the lookbook had the specific point id
$this->db->select('shop');
$this->db->from('shopify_lookbook');
$this->db->where_in('lookbook_id', 'SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid');
The problem is the query it generate
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN('SELECT lookbook_id FROM shopify_point WHERE point_id = 543')
It will give blank but when I try in mysql without '' in IN() like below
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN(SELECT lookbook_id FROM shopify_point WHERE point_id = 543)
It returns the shop name that I want. How can I erase '' in $this->db->where_in()
You might use where instead and to construct your IN clause there:
$this->db->where('lookbook_id IN (SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid)', NULL, FALSE);

how to use the function time in query myql

i have this query :
SELECT *
FROM news
WHERE STATE LIKE 'SI'
AND data<'".time()."'
ORDER
BY data DESC
limit 0,1
and i would like to know if the function time it's correct because there is an error on synthase.
thank's you !
There are reserved words in MySQL which you cannot use as column names without clearly indicating they are names. See:
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Both 'data' and 'date' are reserved words. Use back ticks to indicate they are used as names:
$query = "SELECT *
FROM `news`
WHERE `STATE` LIKE 'SI'
AND `data` < '".time()."'
ORDER
BY `data` DESC
LIMIT 0,1
or better, in my opinion, use better column names:
$query = "SELECT *
FROM newsItems
WHERE itemState LIKE 'SI'
AND creationDate < '".time()."'
ORDER
BY creationDate DESC
LIMIT 0,1";
As you can see I had to guess what the columns really stand for, because it's not directly clear from the names. It should be, because that's what they are there for.
TIME() is a function in which you also need to pass your parameter.
So, instead of using it blank like:
select TIME();
you need to use it in this way:
select TIME(now());
Note: In your query, you need to pass like (only if you have datetime field in your table):
AND time(data) < time(now())

What is the correct MySQL syntax to retrieve data with multiple parameters

I am retrieving data from a database with php and MySQL as follows
$query = mysql_query("SELECT * FROM pictures WHERE (title LIKE '%$Search%' OR keywords LIKE '%$Search%') AND approved = 'YES' ORDER BY title ASC");
The query is correct and there are no errors and the query works fine for "title LIKE '%$Search%'" but the parameter "OR keywords LIKE '%$Search%'" is not retrieving data. The parameter "AND" also works correctly.
The keywords are stored in the database for example "pizza, restaurants, take away" but I don't see that is a problem.
My question is "What is the correct syntax for applying the "OR" parameter?
Remove the brackets around (title LIKE '%$Search%' OR keywords LIKE '%$Search%')
Those are generally used for subqueries.
$query = mysql_query("
SELECT * FROM pictures
WHERE title LIKE '%$Search%'
OR keywords LIKE '%$Search%'
AND approved = 'YES'
ORDER BY title ASC
");
https://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Here is an example of a subquery, and pulled from the manual on MySQL.com:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
Edit:
Or try a different quoting method:
$query = mysql_query("
SELECT * FROM pictures
WHERE title LIKE '".%$Search%."'
OR keywords LIKE '".%$Search%."'
AND approved = 'YES'
ORDER BY title ASC
");
You could also try escaping your data:
$Search = mysql_real_escape_string($Search);
as an example. I don't know how you're assigning that variable.
phpMyAdmin test edit:
This is what I used inside phpMyAdmin:
SELECT * FROM table
WHERE col1 LIKE '%pizza%'
OR col2 LIKE '%pizza%'
AND col3 = 'YES'
ORDER BY col1 ASC
using pizza as the search keyword seeing that $Search will be based on the same keyword for you, where columns contain "large pizza" in one, and "pizza, take away, restaurants" in another.
Remember that, whatever you're using/assigning $Search to, must reside inside all your queried columns.
You may also want to make use of explode().
Here is an example pulled from https://stackoverflow.com/a/15289777/
<?php
$search = 'Gold Chain Shirt';
$bits = explode(' ', $search);
$sql = "SELECT name FROM product WHERE name LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";
The above will generate this query:
SELECT name FROM product WHERE name LIKE '%Gold%' OR name LIKE '%Chain%' OR name LIKE '%Shirt%'
Sorry for taking some time but this is my working answer to my own question... not the prettiest syntax but it works without any string functions or explode functions. MySql can handle keywords quite well without any other functions being included:
$query = mysql_query("SELECT * FROM pictures
WHERE
title LIKE '%$Search%' AND featured IS NOT NULL AND streetview IS NOT NULL AND (id_user > '1') AND (status = '1')
OR
keywords LIKE '%$Search%' AND featured IS NOT NULL AND streetview IS NOT NULL AND (id_user > '1') AND (status = '1') ORDER BY title ASC");
Thank you all for your contributions

using search firstname and last name mixed php query

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 ");

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

Categories