How to create a common search bar in PHP Mysql - php

I am working on an application in which i want to create a search bar which will be able to search for individual users as well as brands.
For this i have written a query like this :
SELECT * FROM temp_users,brand_pages WHERE firstname LIKE'%$find%' or lastname LIKE '%$find%' or name LIKE '%$find%'
I checked the query but it is not as needed. What is the way it can be done ??
Should i use joins ??

Yes you need join.
Try this:
SELECT *
FROM temp_users AS TU
INNER JOIN brand_pages AS BP
WHERE (TU.firstname LIKE "%$find%" OR TU.lastname LIKE "%$find%" OR BP.name LIKE "%$find%")
Edit: If you are using this query in PHP and trying to use the variable $find inline, use double quotes (not single quotes).

Related

How to search the multiple keywords in multiple columns? [duplicate]

I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.

FIND_IN_SET with like clause mysql

I have a MySQL table where I am using comma separated values like:
user_name books
abc PHP,Java
xyz Net,Shift,PHP
I can handle comma-separated value searching using the FIND_IN_SET function of MySQL.
Now books names are auto suggested means if any one search with keyword "J" , system should search and match the word Java and give result of user "abc".
I tried to use like clause with FIND_IN_SET but it's not working.
Does anyone have any suggestions?
Not a clean solution but you can use something like this:
SELECT * FROM `library` WHERE `books` LIKE 'J%' OR `books` LIKE '%,J%'

Searching database mysql

I created a stored procedure to search my database by name. I have many text files saved in database named like: a.txt, a1.txt,a2.txt,a3.txt,...
When i run my stoerd procedure only 1 resoult is showing.
SELECT * FROM cars WHERE name LIKE CONCAT ('%', search ,'%');
What is the problem here?
Your problem solution is
SELECT * FROM cars WHERE name LIKE CONCAT ('%'. $search . '%');
or if you are using directly then on sql server then
SELECT * FROM cars WHERE name LIKE CONCAT ('%search%');
If $search (note the dollar sign) is a PHP variable (I assume that because you tagged the question with "php"), you don't have to use the SQL function CONCAT but join the strings in PHP and send it all to MySQL:
$sql = 'SELECT * FROM cars WHERE name LIKE "%'.$search.'%"';
That's because CONCAT will make your % treated as a character, not a search criterion.
try this
SELECT * FROM cars WHERE name LIKE '%$search%' ;
or this
WHERE name LIKE '$search'

php mysql query issue

here's my table:
and I want get the customers which have some values of for/category fields which is comma separated..
I am trying something like this:
SELECT * FROM `customers` WHERE `for` LIKE ('%AMC PHD & WWS%' OR '%Rostfrei%' OR '%Thermopac%')
but its giving empty result.
RedFilter's SQL is correct, but you should also know that "for" is a MySQL reserved word. You should avoid using it as a column name, or wrap it in backticks when you use it:
SELECT *
FROM customers
WHERE `for` LIKE '%AMC PHD & WWS%'
OR `for` LIKE '%Rostfrei%'
OR `for` LIKE '%Thermopac%';
The alternative, typing the column name once is:
SELECT * FROM customers WHERE `for` REGEXP 'AMC PHD \& WWS|Rostfrei|Thermopac';
Try:
SELECT *
FROM customers
WHERE for LIKE '%AMC PHD & WWS%'
or for LIKE '%Rostfrei%'
or for LIKE '%Thermopac%'

How to make search engine query in Mysql&PHP?

I want to make a search engine in an intranet. Now i use this clause in PHP.
$k = explode(" ",$_GET[key]);
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
But it seems doesn't work. Do you know where is wrong?
Thanks in advance.
Edit:
$sql = "select * from entreprise, site, salarie where entreprise.*, site.*, salarie.* like '%$k[0]%' or '%$k[1]%'";
I have modified the query clause. With this code, i think you can know what i want to do.
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
It's hard to exactly see what you're trying to do, but you need, in your SQL query, to specify :
on which tables you are working, with a from clause
on which fields the search has to be done, in the where clause.
how the data between employees and enterprises are related :
do you want to search for entreprises and their employees ?
for employees and there enterprises ?
for all enterprises and the employees when the employee or the enterprise contains the words ?
You could use something like this to search for entreprises that contain the word, and get their employees to :
select *
from entreprise
inner join employee on employee.id_entreprise = entreprise.id
where entreprise.name like '%word%'
or entreprise.description like '%word%';
Or, to search for employees that match the criteria and get their entreprise too :
select *
from employee
inner join entreprise on entreprise.id = employee.id_entreprise
where employee.name like '%word%';
(just some ideas -- you'll have to build from there !)
This:
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
is not valid SQL. It is hard to guess what you want to do, but I'm trying anyway: you want to find employees, and search them by name or by enterprise that employs them. Is that the case? Or do you want to search employess and/or enterprises?
EDIT
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
Ok, first of all you should realize that SQL is probably not the best tool for this job. See the other commenter - his suggestions about sphinx and friends are good. But still, if you really want to:
$sql = '
SELECT e.id, e.name
FROM enterprise e
-- first, look in column1
WHERE e.column1 LIKE '."'%".$k[0]."%'".'
OR e.column1 LIKE '."'%".$k[1]."%'".'
...etc for all entries in k...
OR e.column1 LIKE '."'%".$k[N]."%'".'
-- then, look in column2
OR e.column2 LIKE '."'%".$k[0]."%'".'
OR e.column2 LIKE '."'%".$k[1]."%'".'
...and so on and so forth for all entries in $k and all columns in enterprise...
UNION ALL
SELECT s.id, s.name
FROM salarie s
WHERE ...and the same for columns of salarie...
...
UNION ALL
...any other tables you want to search...
';
As you can see, not something that makes you happy.
Another approach that might give you more joy is having some overnight job to scan all rows in the tables you're interested in, parse the texts you want to search into separate words, and store those in a keyword table, and storing the association between an object from the source database and the keyword in a separate table. You can then search the keyword table and use the id's and table names you find for a collection of keywords to build the actual query to retrieve those rows. This is what I do, and it works great. It works better because there is a relatively small amount of words that you will encounter, whereas the collection of objects is quite possible very large.

Categories