Jump to content

search site (3+ tables)


phorcon3

Recommended Posts

I wanna do a search on three or more tables at once, but how would I do this?

 

here's a mysql sample:

 

CREATE TABLE `table1` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
`col2` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

CREATE TABLE `table2` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

CREATE TABLE `table3` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
`col2` varchar(255) NOT NULL default '',
`col3` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

 

so, what I need to do is:

 

a) find the total number of results, so I suppose I will have to use Count(*) as Count somewhere?

 

and

 

b) find out from which table I got the current results

 

php script:

 

<?php

$search = addslashes($_GET['search']);

$a = mysql_query("");//query -> ex: SELECT on.col1, on.col2, tw.col1, tr.col1, tr.col2, tr.col3 FROM table1 as on, table1 as tw, table3 as tr WHERE on.col1 LIKE '%$search%' OR on.col2 LIKE '%$search%' etc. ORDER BY id DESC LIMIT 0,10
while($b = mysql_fetch_assoc($a))
{
//find out from which table!

echo 'this result is from table => '.$tablename;
}

echo 'the total search result is: '.$total;

?>

Link to comment
https://forums.phpfreaks.com/topic/92319-search-site-3-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.