我正在为我的预订系统构建搜索引擎。我已成功设法构建一个查询一个数据库的搜索引擎,但我想知道如何使用相同的搜索引擎查询多个数据库。
第一个数据库($ conn)包含以下内容:
第二个数据库($ conn2)包含以下内容: - 房间 - 预定日期 - 开始时间 - 时间结束 - 备注
我尝试执行以下操作,但是出现了以下错误消息:
警告:mysqli_num_rows()期望参数1为mysqli_result, 在第74行的C:\ xampp \ htdocs \ results.php中给出的布尔值
这是我的代码:
<?php
$server="localhost";
$username="root";
$password="";
$dbname="equipment";
$conn=mysqli_connect($server,$username,$password,$dbname);
$conn2=mysqli_connect($server,$username,$password,$dbname);
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="resultsstyle.css">
<link rel="icon" href="Icon.jpg">
<title>Search</title>
</head>
<body>
<div class="header">
<img class="icon" src="Icon.jpg">
<div class="search">Search </div>
</div>
<div class="select">Results </div>
<?php
if (isset($_POST['submit-search'])){
$search=mysqli_real_escape_string($conn,$_POST['search']);
$sql="SELECT * FROM bookings WHERE equipment LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn, $sql);
$queryResult=mysqli_num_rows($result);
if(empty($_POST['search'])){
echo '<span class="empty">Search box was empty. Please try again.</span>';
}else{
if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Equipment: <span class='first'>".htmlspecialchars($row['equipment'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}
}else{
echo "<span class='none'>No results were found.</span>";
}
}
}
?>
<?php
if (isset($_POST['submit-search'])){
$search=mysqli_real_escape_string($conn2,$_POST['search']);
$sql="SELECT * FROM bookings2 WHERE room LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn2, $sql);
$queryResult=mysqli_num_rows($result);
if(empty($_POST['search'])){
echo '<span class="empty">Search box was empty. Please try again.</span>';
}else{
if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Room: <span class='first'>".htmlspecialchars($row['room'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}
}else{
echo "<span class='none'>No results were found.</span>";
}
}
}
?>
我拼命尝试了一切,但同样的错误信息不断出现。任何帮助,将不胜感激。谢谢
答案 0 :(得分:0)
首先,我建议不要使用存储的过程语句,因为你对任何sql注入都是开放的。阅读更多相关信息here。
要回答您的问题,“如何使用相同的搜索引擎查询多个数据库”将是LEFT JOIN
。有了这个,您可以在一个查询中加入两个或多个数据库。例如:SELECT d.*, db.* FROM database AS d LEFT JOIN database2 AS db ON d.booking_date = db.booking_date WHERE equipment = %search%
。在这里,我选择了加入*
和database
的数据库中的所有内容(database2
)在每个属性中共有的属性(booking_date
)。因此,您的查询如下所示:SELECT b.*bk.* FROM bookings AS b LEFT JOIN bk AS bookings2 ON b.start_time = bk.start_time WHERE (b.equipment OR bk.rooms) LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'
您可以阅读有关LEFT JOIN
here.