gpt4 book ai didi

php - 在mysql中搜索多个表

转载 作者:行者123 更新时间:2023-11-30 22:01:55 24 4
gpt4 key购买 nike

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

我有上面的代码来搜索客户姓名或电话号码以显示客户详细信息。在这里,我面临一个问题,我总共有 9 个表,例如 aug2016、sep206。我试过 join union none 对我不起作用总是给我错误。谁能告诉我如何一次搜索所有表格?谢谢

最佳答案

我能想到的唯一方法是你需要使用 union 查询所有表

尝试类似的东西

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

关于php - 在mysql中搜索多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43095275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com