dsunj08246 2017-03-29 13:42
浏览 68

在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>

I have the above code to search for customer name or phone no itto showup the customer details. Here I am facing a issue I have total 9 tables like aug2016,sep206. I tried joins union none works for me always giving me error. Can some one tell me how I can search all the tables at a time? Thanks

</div>
  • 写回答

1条回答 默认 最新

  • doushi8186 2017-03-29 14:39
    关注

    the only way I could think of is that you need to query on all tables using union

    try something like that

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

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)