我想使用 PHP 和 SQL 查询多个表,并在单个 HTML 页面中返回格式。
这是我目前得到的:
.HTML:
<form name="searchForm" method="POST" action=""> <!-- My new PHP file would go here -->
Search for: <input type="text" name="aSearch">
<input type="submit" name="searchButton" value="Search">
</form>
控制器 1 (PHP(:
// Connect to DB
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new MySQLi($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Collect Data phase:
// If an input has been given remove unnecessary characters
if(isset($_POST["aSearch"])) {
$searchq = $_POST["aSearch"]; //searchq allows the input to search for part of a word
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //Can only search words
// Select statements if the searched word and insectName match
$sql = "SELECT * FROM insectt WHERE insectName LIKE '%$searchq%'";
}
// Tests if the code been inserted
if ($conn->query($sql)=== TRUE){
echo "The rows you have searched for are:";
} else {
echo "Connection failed: ";
echo $conn->error;
}
// Show fields
$result = $conn->query($sql);
// Output data of each row
if ($result-> num_rows> 0) {
readfile("ViewReturn.html"); //Output it in an html file
while($row = $result-> fetch_assoc()) {
echo "Insect Name: ".$row["insectName"]. "<br><br>";
}
} else {
echo "0 results";
}
// Close connections
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
echo "There was an error with MySQLi";
} else {
echo "Closing Connections";
}
$conn-> close();
exit();
?>
控制器 2 (PHP(:
// Connect to DB
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new MySQLi($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Collect Data phase:
// If an input has been given remove unnecessary characters
if(isset($_POST["aSearch"])) {
$searchq = $_POST["aSearch"]; //searchq allows the input to search for part of a word
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //Can only search words
// Select statements if searched
$sql = "SELECT * FROM birdt WHERE birdName LIKE '%$searchq%'";
}
// Tests if the code been inserted
if ($conn->query($sql)=== TRUE){
echo "The rows you have searched for are:";
} else {
echo "Connection failed: ";
echo $conn->error;
}
// Show fields
$result = $conn->query($sql);
// Output data of each row
if ($result-> num_rows> 0) {
readfile("ViewReturn.html"); //Output it in an html file
while($row = $result-> fetch_assoc()) {
echo "Bird Name: ".$row["birdName"]. "<br><br>";
}
} else {
echo "0 results";
}
// Close connections
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
echo "There was an error with MySQLi";
} else {
echo "Closing Connections";
}
$conn-> close();
exit();
?>
SQL 表:
birdt: insectt:
╔════════╦═══════════╗ ╔══════════╦════════════╗
║ birdID ║ birdName ║ ║ insectID ║ insectName ║
╠════════╬═══════════╣ ╠══════════╬════════════╣
║ 1 ║ Gull ║ ║ 1 ║ cricket ║
║ 2 ║ Hawk ║ ║ 2 ║ bee ║
║ 3 ║ Pigeon ║ ║ 3 ║ flea ║
╚════════╩═══════════╝ ╚══════════╩════════════╝
目前,我可以通过更改HTML表单中的操作(我可以从下拉列表中执行此操作(来分别搜索两个表,并在不同的HTML页面中分别返回查询结果,但仅此而已。
我想知道的是,是否有办法从单个控制器查询两个SQL表(并将其作为我的html表单中的新控制器(,它可以查询我的两个表并在单个HTML页面中返回搜索结果。因此,如果我要搜索与鸟或插图表中的记录名称匹配的单个关键字 - 或两者兼而有之 - 它将返回它。
可能需要更改的是我的 PHP 文件中的 SQL 查询,但我不确定将其更改为什么。
我真的不知道该怎么做,网上的信息很混乱,所以我会接受任何帮助。
提前感谢您的任何帮助。
你可以像这样使用多个查询
$sql = "SELECT * FROM birdt WHERE birdName LIKE '%$searchq%';";
$sql .= "SELECT * FROM insectt WHERE insectName LIKE '%$searchq%'";
并执行mysql_multi_query()
欲了解更多信息,请访问 : http://www.w3schools.com/php/func_mysqli_multi_query.asp