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: -
for ($x = 0; $x < count($keys); $x++) {
-
$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]%\"";
-
$result = mysql_query($querystr);
-
(...)
-
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? :'(
If you want a PHP solution, it could be as simple as: - <?php
-
$tables = array("table1", "table2");
-
$keywords = array("php", "mysql", "shoelaces");
-
-
foreach($tables as $_table) {
-
foreach($keywords as $_keyword) {
-
$sql = "SELECT stuff FROM `{$_table}` WHERE other_stuff...";
-
}
-
}
-
?>
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: - ( SELECT stuff FROM {$table1}
-
WHERE description LIKE '%{$key}%'
-
OR ordine LIKE '%{$key}%')
-
UNION ALL
-
( SELECT stuff FROM {$table2}
-
WHERE description LIKE '%{$key}%'
-
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? - 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: - <?php
-
$tables = array("table1", "table2");
-
$keywords = array("php", "mysql", "shoelaces");
-
-
foreach($tables as $_table) {
-
foreach($keywords as $_keyword) {
-
$sql = "SELECT stuff FROM `{$_table}` WHERE other_stuff...";
-
}
-
}
-
?>
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: - ( SELECT stuff FROM {$table1}
-
WHERE description LIKE '%{$key}%'
-
OR ordine LIKE '%{$key}%')
-
UNION ALL
-
( SELECT stuff FROM {$table2}
-
WHERE description LIKE '%{$key}%'
-
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? - 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).
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".
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |