473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

search engine on multiple tables?

10
Hello everyone,

I'm trying to do a little search engine for my website.
Like often, I've the problem to operate on multiple tables.
This is the part of code that interests my problem. It works but I need to search in more tables than one:

Expand|Select|Wrap|Line Numbers
  1. for ($x = 0; $x < count($keys); $x++) {
  2. $querystr = "SELECT img,link,numclick,description,ordine FROM $table1 WHERE description = \"$keys[$x]\" OR description LIKE \"%$keys[$x]%\" OR ordine = \"$keys[$x]\" OR ordine LIKE \"%$keys[$x]%\"";
  3. $result = mysql_query($querystr);
  4. (...)
  5.  
I've tried with this method that was working in another situation but not now. Maybe because it is in a for cycle?

(SELECT *
FROM $table1)
UNION ALL
(SELECT *
FROM $table2)
UNION ALL
etc.

How can I do? :'(
Jul 30 '10 #1

✓ answered by Atli

If you want a PHP solution, it could be as simple as:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tables = array("table1", "table2");
  3. $keywords = array("php", "mysql", "shoelaces");
  4.  
  5. foreach($tables as $_table) {
  6.     foreach($keywords as $_keyword) {
  7.         $sql = "SELECT stuff FROM `{$_table}` WHERE other_stuff...";
  8.     }
  9. }
  10. ?>
You should also be able to use UNION ALL to execute those queries in larger batches. I can't see a reason why something like this wouldn't work:
Expand|Select|Wrap|Line Numbers
  1. (   SELECT  stuff FROM {$table1} 
  2.     WHERE   description LIKE '%{$key}%'
  3.     OR      ordine LIKE '%{$key}%')
  4. UNION ALL
  5. (   SELECT  stuff FROM {$table2} 
  6.     WHERE   description LIKE '%{$key}%'
  7.     OR      ordine LIKE '%{$key}%')
Which is something you could easily build using something similar to the above PHP loops.

There may be a better way to search both tables at once. If you show us how they look like, we could check it out.

Also, isn't this a tad redundant?
Expand|Select|Wrap|Line Numbers
  1. WHERE description = \"$keys[$x]\" OR description LIKE \"%$keys[$x]%\"
The LIKE clause would also catch what the first boolean search is looking for, which makes it a duplicate. Unless you've got some performance reasons for it? (Although, seeing as this is a description, and thus unlikely to contain only a single keyword, and the performance gain from avoiding the LIKE is doubtfully reason enough anwyas... unless your running a Google-size search engine :P).

2 5220
Atli
5,058 Expert 4TB
If you want a PHP solution, it could be as simple as:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $tables = array("table1", "table2");
  3. $keywords = array("php", "mysql", "shoelaces");
  4.  
  5. foreach($tables as $_table) {
  6.     foreach($keywords as $_keyword) {
  7.         $sql = "SELECT stuff FROM `{$_table}` WHERE other_stuff...";
  8.     }
  9. }
  10. ?>
You should also be able to use UNION ALL to execute those queries in larger batches. I can't see a reason why something like this wouldn't work:
Expand|Select|Wrap|Line Numbers
  1. (   SELECT  stuff FROM {$table1} 
  2.     WHERE   description LIKE '%{$key}%'
  3.     OR      ordine LIKE '%{$key}%')
  4. UNION ALL
  5. (   SELECT  stuff FROM {$table2} 
  6.     WHERE   description LIKE '%{$key}%'
  7.     OR      ordine LIKE '%{$key}%')
Which is something you could easily build using something similar to the above PHP loops.

There may be a better way to search both tables at once. If you show us how they look like, we could check it out.

Also, isn't this a tad redundant?
Expand|Select|Wrap|Line Numbers
  1. WHERE description = \"$keys[$x]\" OR description LIKE \"%$keys[$x]%\"
The LIKE clause would also catch what the first boolean search is looking for, which makes it a duplicate. Unless you've got some performance reasons for it? (Although, seeing as this is a description, and thus unlikely to contain only a single keyword, and the performance gain from avoiding the LIKE is doubtfully reason enough anwyas... unless your running a Google-size search engine :P).
Jul 31 '10 #2
deiv82
10
Hi thank you very much for your answer, unluckly i've seen just now...
i was just trying to use your first advice, the one of the array... it seems to be good and i don't get any error, just i don't understand why i get always only the last table in the array, for example like this

$tables = array("table1", "table2");

i get only the records of "table2".
Aug 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Zaphod Beeblebrox | last post by:
As much of this question relates to mysql, it may be OT? I'm trying to make a search engine for a reasonably complex database that was originally developed by someone else in Access. I've ported...
5
by: LRW | last post by:
Well, I have another question that's hard to ask, so I may end up over explaining the question. I have about 40 tables, all with a similar column structure. I want to be able to search through...
8
by: Rod | last post by:
Hi, i am doing a ecommerce website and would like to implement a search engine to find products. All the serach engine I have found on the web are parsing html page! This is not what i want. i...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
0
by: Mark Goldin | last post by:
I am working on a search engine. An aspx page shows some data. The user will type something in and click a button to search. I want to create a server solution for the search engine. So the search...
1
by: John | last post by:
Hello, We are developing an application against an MS SQL Server 2000 database which requires that we implement full-text searching across columns in multiple tables. The research that we have...
1
by: none | last post by:
I am trying to optimize a search engine. let's say I have 3 tables. one has titles, one has words, and an index where each title is broken into words (title_id and word_id) I want to be able...
3
by: gyap88 | last post by:
My search engine in visual basic 2005 has 4 textbox for users to input values, named textbox1,textbox2,textbox3,textbox4. I have a string assigned to each of the textbox named...
0
by: redpears007 | last post by:
Morning all! :) I have a database with multiple linked tables. I have created a search form with one txt box, for entering search criteria, and a listbox for each of the tables to isplay the...
1
by: samvb | last post by:
Hi, I am trying to include a site wide search engine in my site. The site is fully mysql database driven with tables that have different structures. In site, i have news.php that deals with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.