Chi Shen Chi Shen - 27 days ago 9
MySQL Question

display "result not found" if no result in database

i want to display a message "result not found" if user search something that do not exits in database.

here is my code but it didnt works.

can somebody tell me what's wrong with my code?

<?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>

Answer

Check the amount of rows and then write your code based on the result.

$rows=mysqli_num_rows($rsSearch); 

if($rows > 0) {

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

// code for results

} 

} else {

// legend for no results

}

Edit: you don't need to ask the mysql result array whether the line is populated or empty, the line itself will never be empty, it will simply not exist in the array if the query didn't find any data.