gpt4 book ai didi

php - 使用 mysqli 多重查询显示搜索结果

转载 作者:行者123 更新时间:2023-11-29 20:14:27 25 4
gpt4 key购买 nike

如何使用 mysqli 多重查询显示搜索结果。我想显示列表详细信息表和用户表中的值。这是我的代码:

                $searchquery="SELECT * FROM `listing-details` WHERE `listing-address` LIKE '%" . $address . "%' AND `listing-address-street` LIKE '%" . $street . "%' AND `listing-address-barangay-id` LIKE '%" . $barangay . "%'";
$searchquery.= "SELECT `user.user-username`, `user.user-firstname`, `user.user-lastname`, `listing-details.user-username` FROM `user`, `listing-details` WHERE `listing-details.user-username`=`user.user-username`";

if (mysqli_multi_query($conn, $searchquery)) {
do {
if ($result=mysqli_store_result($conn,$searchquery)){
while($row=mysqli_fetch_row($result)){
$listingid =$row['listing-id'];
$username =$row['user-username'];
$listingbedquantity =$row['listing-bedquantity'];
$listingbedtype =$row['listing-bedtype-id'];
$listingguestsquantity =$row['listing-guestsquantity'];
$listingplacetype =$row['listing-placetype-id'];
$listingpropertytype =$row['listing-propertytype-id'];
$listingbathroomquantity =$row['listing-bathroomquantity'];
$listingaddress =$row['listing-address'];
$listingstreet =$row['listing-address-street'];
$listingbarangay =$row['listing-address-barangay-id'];
$listingamenities =$row['listing-amenities-basic-id'];
$listingsafetyamenities =$row['listing-amenities-safety-id'];
$listingsaphotos =$row['listing-amenities-safety-photos-id'];
$listingspace =$row['listing-space-id'];
$listinglandmark =$row['listing-landmark'];
$listingpreferences =$row['listing-preferences-id'];
$listingphotoset =$row['listing-photosset-id'];
$listingexperience =$row['listing-experience-id'];
$listingfrequency =$row['listing-frequency-id'];
$listingstartdate =$row['listing-startdate'];
$listingrate =$row['listing-rate-id'];
$listingprice =$row['listing-price'];
$listingrules =$row['listing-rules-id'];
$listingtitle =$row['listing-title'];
$listingdescription =$row['listing-description'];
$firstname =$row['user-firstname'];
$lastname =$row['user-lastname'];

echo "<ul>\n";
echo "<li>"."<a href=\"search.php?id=$listingid\">" . "<h2>" . $listingtitle . "</h2></a></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<ul>\n";
echo "<li>"."<a href=\"search.php?id=$listingid\">" . "<h2>" . $listingtitle . "</h2></a></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<li style='float:right;'>By: " . $firstname . " " . $lastname . "</i></li>";
echo "</ul>";
echo "<hr width='80%' noshade='1'>";

}
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}

但是,当我运行它时,页面会加载,但结果不会显示。其目的是能够显示列表详细信息表中的列表详细信息,该列表按用户表中的完整名称列出。两个表都有 user-username 列作为公共(public)键。

最佳答案

像许多学习者一样,您使用了错误的工具,只是因为您不知道正确的工具。

您不需要多重查询(实际上您永远不需要它),而是JOIN

只需将两个怪物查询重写为这样的连接

$searchquery="SELECT l.*, u.`user-username`, u.`user-firstname`, u.`user-lastname`
FROM `listing-details` l, user u
WHERE l.`user-username`=u.`user-username`
AND `listing-address` LIKE ...";

另外,你的引用是错误的。

关于php - 使用 mysqli 多重查询显示搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874680/

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