在mysql中搜索多个表

时间:2017-03-29 13:42:42

标签: php mysql



<?php
    mysql_connect("localhost", "dbl", "pass") or die("Error connecting to database: ".mysql_error());
    /*
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password
         
        if connection fails it will stop loading the page and display an error
    */
     
    mysql_select_db("trackerp_excl") or die(mysql_error());
    /* tutorial_search is the name of database we've created */
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}


</style>
</head>
<body>
<?php
    $query = $_GET['query']; 
    // gets value sent over search form
     
    $min_length = 3;
    // you can set minimum length of the query if you want
     
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
         
        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;
         
        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection
         
      $raw_results = mysql_query("SELECT * FROM june2016
            WHERE (`CUSTOMER_NAME` LIKE '%".$query."%') OR (`PHONE_NO` LIKE '%".$query."%')") or die(mysql_error());
             
        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table
         
        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
   echo "<table>
  <tr>
<th>Date</th>
<th>Name</th>
<th>PHONE NO</th>
<th>ALT NO</th>
<th>EMAIL</th>
<th>AMOUNT</th>
<th>PAYMENT STATUS</th>
<th>SALES AGENT</th>
<th>MODE OF PAYMENT</th>
<th>PLAN</th>
<th>INVOICE NO</th>
  </tr>";      
        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
             
            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
             

                echo "<tr><td>".$results['DATE']."</td><td>".$results['CUSTOMER_NAME']."</td><td>".$results['PHONE_NO']."</td><td>".$results['ALT_NO']."</td><td>".$results['EMAIL']."</td><TD>".$results['AMOUNT']."</td><TD>".$results['PAYMENT_STATUS']."</td><td>".$results['SALES_AGENT']."</td><TD>".$results['MODE_OF_PAYMENT']."</td><td>".$results['PLAN']."</td><td>".$results['INVOICE_NO']."</td>";

                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
             
        }
        else{ // if there is no matching rows do following
            echo "No results";
        }
         
    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }echo "</table>";
?>
</body>
</html>
&#13;
&#13;
&#13;

我有上面的代码来搜索客户名称或电话没有显示客户详细信息。在这里,我面临一个问题,我总共有9个表,如aug2016,sep206。我尝试加入工会没有为我工作总是给我错误。有人可以告诉我如何一次搜索所有表格吗?感谢

1 个答案:

答案 0 :(得分:0)

我能想到的唯一方法是你需要使用union

查询所有表

尝试类似的东西

select * from table1 where username="user1"
union
(select * from table2 where username="user1")
union
(select * from table3 where username="user1")
...