gpt4 book ai didi

php - 如果数据库中没有结果,则显示 "result not found"

转载 作者:行者123 更新时间:2023-11-29 07:09:03 24 4
gpt4 key购买 nike

如果用户搜索数据库中不存在的内容,我想显示一条消息“未找到结果”。
这是我的代码,但它不起作用。

有人可以告诉我我的代码有什么问题吗?

 <?php
include 'database_conn.php';

$eventCat = $_GET['catID'];
$eventVenue =$_GET['venueName1'];

$sqlSearch = " SELECT * FROM te_events

WHERE catID = '$eventCat' AND venueID = '$eventVenue'";


$rsSearch = mysqli_query($conn,$sqlSearch) or die(mysqli_error($conn));


?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<thead>
<th>Title</th>
</thead>
<?php
while ($row =mysqli_fetch_assoc($rsSearch)){
//extract each field$
$id = $row ["eventID"];
$title = $row ["eventTitle"];
$desc = $row ["eventDescription"];
$venueId = $row ["venueID"];
// $venue = $row ["venueName"];
// $location = $row ["location"];
$categoryId = $row ["catID"];
//$category = $row ["catDesc"];
$eStart = $row ["eventStartDate"];
$eEnd = $row ["eventEndDate"];
$ePrice = $row ["eventPrice"];


if(!empty($row)){

//start a row
echo"<tr>\n";
//output the url
echo"<td>\n";
echo"<div><a href =\"allDetails.php?eventID=$id\">
$title</a></div>\n";
echo"</td>\n";
}
else{
echo "No event found! Please select other option.\n";



}

}
mysqli_free_result($rsSearch);
mysqli_close($conn);

?>

</body>
</html>

最佳答案

检查行数,然后根据结果编写代码。

$rows=mysqli_num_rows($rsSearch); 

if($rows > 0) {

while ($row = mysqli_fetch_assoc($rsSearch)){

// code for results

}

} else {

// legend for no results

}

编辑:你不需要询问mysql结果数组该行是否已填充或为空,该行本身永远不会为空,如果查询没有找到任何数据,它就不会存在于数组中。

关于php - 如果数据库中没有结果,则显示 "result not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530909/

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