Gntr
Gntr

Reputation: 19

How to add Search message "Not found" in my php code?

I want to get "Car not found!" message in this code. Please help me... How to get this message?

<?php 
//Query for Listing count
$brand=$_POST['brand'];
$fueltype=$_POST['fueltype'];
$sql = "SELECT id from tblvehicles where tblvehicles.VehiclesBrand=:brand and tblvehicles.FuelType=:fueltype";
$query = $dbh -> prepare($sql);
$query -> bindParam(':brand',$brand, PDO::PARAM_STR);
$query -> bindParam(':fueltype',$fueltype, PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=$query->rowCount();
?>
<p><span><?php echo htmlentities($cnt);?> Listings</span></p>
</div>
</div>

Where to write echo to get this message?

$sql = "SELECT tblvehicles.*,tblbrands.BrandName,tblbrands.id as bid  from tblvehicles join tblbrands on tblbrands.id=tblvehicles.VehiclesBrand where tblvehicles.VehiclesBrand=:brand and tblvehicles.FuelType=:fueltype";
$query = $dbh -> prepare($sql);
$query -> bindParam(':brand',$brand, PDO::PARAM_STR);
$query -> bindParam(':fueltype',$fueltype, PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{  ?>

Upvotes: 1

Views: 187

Answers (2)

Martin
Martin

Reputation: 2414

You already have your if statement to check if your rowcount > 0 (i.e. it has something to return), and print out the result. You can just add an else to that where you echo out your message.

Upvotes: 0

Obsidian Age
Obsidian Age

Reputation: 42304

Inside of an else conditional chained to your $query->rowCount():

if($query->rowCount() > 0) {
    foreach($results as $result) { 
        // Output the successful results
    }
} else {
    echo "Car not found!";
}

Upvotes: 1

Related Questions