Jump to content

Searching multiple tables and rows.


-Karl-

Recommended Posts

I have my code:

<?php
if (isset($_GET['search'])) {

         echo <<<HTML
<center>You searched for {$_GET['search']}</center>
HTML;
if(strlen($_GET['search']) <= 3){
echo 'Search must contain more than 3 characters.';
} else {

$con = mysql_connect("localhost","****","****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
   $query = "SELECT * FROM `news` WHERE (`id` LIKE '%{$_GET['search']}%' OR `title` LIKE '%{$_GET['search']}%' OR `thumb` LIKE '%{$_GET['search']}%' OR `news` LIKE '%{$_GET['search']}%' OR `postedby` LIKE '%{$_GET['search']}%')";
   $run = mysql_query($query);
if(!mysql_num_rows($run)==0){
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
<font color="blue">{$arr['title']}</font><br/>
<font color="white">{$arr['news']}</font><br/>
<a href="#">{$arr['id']}</a><br/><br/><br/>
HTML;
}
      }
} 

if(mysql_num_rows($run)==0){
echo 'No results found';
}
}
}
?>

 

But at the moment it only searches the "news" table, and only columns specific to that table (id, title, thumb, etc).

 

However, I have around 4 tables, with unique columns in each one. I need to change the MySQL query to search each of the tables and the columns in each one, then output the data. I'm not sure how to do this though as I've never tried any joining, or union based queries before.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/195924-searching-multiple-tables-and-rows/
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.