我想使用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查询,但我不确定要将其更改为。
我真的不知道如何解决这个问题,网上的信息很混乱,所以我会接受任何帮助。
提前感谢您的帮助。 :¬)
答案 0 :(得分:0)
您可以使用多个查询
$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