使用关键字在多个表中搜索并在php中显示

时间:2015-08-13 08:25:43

标签: php mysql

我有2张桌子

表1:公司

_______________________________
|comp_id| company_name | Views |
|_______|______________|_______|
| 6     |  facebook    | 102   |
| 7     |  google      | 158   |
| 8     |  gmail       | 150   |
| 9     |  Robert      | 210   |
| 10    |  OIC         |  15   |
|_______|______________|_______|

表2:company_info

_____________________________________________
| id    | comp_id | description |  keywords  |
|_______|_________|_____________|____________|
| 1     | 6       | anything... | work, seo  |
| 2     | 7       | anything... | gossip     |
| 3     | 8       | anything... | usa,uk,uae |
| 4     | 9       | anything... | something  |
| 5     | 10      | anything... | something  |
|_______|_________|_____________| ___________| 

我需要搜索上述所有2个表格中的所有字词,并在搜索关键字中显示comp_idcompany_nameviewsdescription

这是我的PHP代码:

<?php
  $query = "SELECT comp_id, company_name, description
    FROM `company`
    INNER JOIN `company_info`
      ON company_info.comp_id = company.comp_id
    WHERE `company_name` LIKE '%" . $search ."%'
      OR `keywords` LIKE '%" . $search ."%'
      OR `description` LIKE '%" . $search ."%'";

   $query_run = mysql_query($query);

   while ($query_row = mysql_fetch_assoc($query_run)) {
      $company_name = $query_row['company_name'];
      $company_info = $query_row['description'];
      $company_views = $query_row['views'];
 ?>
   <div class="box effect2">
   <div id="title"><?php echo $company_name; ?></div>
   <div id="thumbnil">
   <img width="100%" height="100%" src="images/logos/9.png">
   </div>
   <div id="descreption">
   <?php echo $company_info; ?>
   <div style="color:#888;margin-top:10px;">
   Views : <?php echo $company_views; ?>
   </div>
   </div>
   </div>
 <?php } ?>

这不起作用;有人可以帮我查询吗?

1 个答案:

答案 0 :(得分:0)

$query = "Your query";
die($query); // To show the query and evalutate what is the error (in a SQL client, phpMyAdmin or wherever...) and when you are sure that the query is PERFECT su romove the die sentence
$query_run = mysql_query($query) ; // Better you can use: mysql_query($sql) or die(mysql_error());

...