简体   繁体   中英

How to give an error when items are not found in database (PHP and Mysql)

I am newbie in PHP as well as mysql. I'm getting stuck with this problem. What I want is to print an error if the items are not found in the database.

Bellow is my code for it:

$searchParam = '%' . $_GET['q'] . '%';
$moment = microtime(true);
$stmt = $db->prepare('SELECT * FROM product WHERE (serial_number LIKE ? OR name LIKE ?) ORDER BY production_date D$'); //fixed
$stmt->execute(array($searchParam, $searchParam));
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
$queryTime = microtime(true) - $moment;


$output = array(
        'query_time' => $queryTime,
        'products' => $products
);

die(json_encode($output));

Thank you so much for your help.

I don't know what you exactly want to achieve and if you are parsing the result in the frontend, but this is a simple solution for your case:

$searchParam = '%' . $_GET['q'] . '%';
$moment = microtime(true);
$stmt = $db->prepare('SELECT * FROM product WHERE (serial_number LIKE ? OR name LIKE ?) ORDER BY production_date D$
$stmt->execute(array($searchParam, $searchParam));
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
$queryTime = microtime(true) - $moment;

if($products) {
    $output = array(
            'query_time' => $queryTime,
            'products' => $products,
            'success' => true
    );
}
else {
    $output = array(
        'query_time' => $queryTime,
        'error' => 'Some error message',
        'success' => false
    );
}

die(json_encode($output));

For a better answer give us some additional info about what you are doing with the response from the php and why it is in json format.

You can simply count the amount of elements returned in the array $products.

if ( count($products) == 0) {
    echo 'No matches found';
}

This all comes after checking if the query succeeded in the first place. If the query fails setting the variables might fail and this if-statement will fail. But this is most likely your solution to checking if no rows have been found on the select statement.

Try this

<?php
$searchParam = '%' . $_GET['q'] . '%';
$moment = microtime(true);
$stmt = $db->prepare('SELECT * FROM product WHERE (serial_number LIKE ? OR name LIKE ?) ORDER BY production_date D$'); //fixed
$stmt->execute(array($searchParam, $searchParam));

$number_of_rows = $stmt->fetchColumn(); 

if($number_of_rows > 0) {
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $queryTime = microtime(true) - $moment;

    $output = array(
            'query_time' => $queryTime,
            'products' => $products
    );
} else {
    $output = array(
            'query_time' => $queryTime,
            'products' => $products,
            'success' => false,
            'error_msg' =>  'Product not found.',
    );
}
die(json_encode($output));
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM