tags:

views:

52

answers:

2

Hello,

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.

+1  A: 

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.

Roland Bouman
@Roland Bouman, i'm sorry for not explaining my ideas. Please see the my edit. Thanks.
garcon1986
Amended my answer. I would go for my second suggestion - make separate tables to implement a keyword index.
Roland Bouman
@thanks so much, Roland, the database keyword search idea is so good. I haven't done something like this. But it's really interesting. Can i ask you some questions if i meet some problems with that? I'm trying to learn and use it.
garcon1986
sure, you can always ask. I check stackoverflow quite regularly so I should notice.
Roland Bouman
@Roland, thanks in advance. I'm wondering if you'are using sphinx+MySQL fulltext, or you just make it by yourself. I'm looking at the documentations on sphinx. Can you give me some guidance on it?
garcon1986
No, I'm not sphinx user.
Roland Bouman
@Roland, so did you build the search engine by yourself? WOW.
garcon1986
garcon1986, if you call it a "search engine", than that's perhaps a bit more than it deserves. I call it a keyword index. It works for me because I have a modest number of objects/documents (less than 100.000) and most of them need to be indexed by title only.
Roland Bouman
+1  A: 

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 !)

Pascal MARTIN
@Pascal MARTIN, I want to search all the things from a database, even there aren't relationships among the tables in the DB. Do you know what i mean and help me out?
garcon1986
This (from what I know) is not quite that simple, at least in SQL : you have to specify on what you are searching... Maybe another solution (like a fulltext indexation/search engine) would be better suited for your need ? I'm thinking about things like Lucene, Solr, Sphinx, ...
Pascal MARTIN
@thanks, so in your opinion, it's not possible to do that with queries like my post?
garcon1986
You could do somehting like that *(but you'll have to list the fields and tables by hand -- same for the joining conditions)*, but I would use another solution, if it was possible and if I the choice ^^
Pascal MARTIN
@Pascal, thanks, it's better using the existing SEs. But integrating them is not an easy thing for me. :(
garcon1986