0

为了清理和整理我的代码,我想添加多个带有字段的表:

id 
title 
description
keywords
link 

但我也希望它们在部分中,所以在 MySql 中我想要不同的表格,例如:“新闻”、“社交网络”和“购物”,但我怎样才能得到呢?这是我的代码:

<?php
 if( count($terms) == 0){ // If no terms entered, stop.
echo "No Search Terms Entered.";
}else{      
// connect
    $connect = mysql_connect("XXX", "XXX", "YYY") or die('Couldn\'t connect to MySQL Server: ' . mysql_error());
    mysql_select_db("theqlickcom_774575_db1", $connect ) or die('Couldn\'t Select the database: ' . mysql_error( $connect ));

/* Query Statement Building - Terms together */
$query = " SELECT * FROM scan WHERE "; 

$terms = array_map('mysql_real_escape_string', $terms);
$i = 0; 
foreach ($terms as $each) {
      if ($i++ !== 0){
            $query .= " AND ";
      }
      $query .= "keywords LIKE '%{$each}%'";
}



$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {

      while ($row = mysql_fetch_assoc($query)) {
            $id = $row['id'];
            $title = $row['title'];
            $description = $row['description'];
            $keywords = $row['keywords'];
            $link = $row['link'];

        echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
                    echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;            
                    echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
                      }
} else {
    /* Query Statement Building - Terms Separate */
    $query = " SELECT * FROM scan WHERE "; 

    $terms = array_map('mysql_real_escape_string', $terms);
    $i = 0; 
    foreach ($terms as $each) {
          if ($i++ !== 0){
                $query .= " OR ";
          }
          $query .= "keywords LIKE '%{$each}%'";
    }

    $query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
    $numrows = mysql_num_rows($query);
    if ($numrows > 0) {

          while ($row = mysql_fetch_assoc($query)) {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];

                echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
                echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;            
                echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
                  }

    } else {
          echo "No results found for \"<b>{$k}</b>\""; 
    }
}
//disconnect
}
?>
4

1 回答 1

0

如果您必须用一个查询搜索多个表

1.使用mysqli

2.在查询中使用UNION ALL

  1. 连接表

  2. 使用mysql程序

于 2012-07-30T16:44:18.190 回答