php pdo 搜索验证

php pdo search validation

这是我的搜索代码

如果用户输入的值不存在,我需要对此进行验证,然后显示消息数据不可用。

如果用户输入不可用,我需要验证码来回显数据未找到消息。

我的意思是如果找到值则回显否则显示未找到消息。

我尝试了下面的代码,但这个回显数据和消息也是如此。

 <?php
    if($_POST && isset($_POST['submit']))
    {                       
        $result=$db->prepare('SELECT scheme_name,city,coupon,amount,receipt_no,book_no2 FROM scheme_master WHERE receipt_no=:receipt_no');
        $result->bindParam(':receipt_no',$_POST['receipt_no']);
        $result->execute(); 
        $data = $result->fetchAll();
        $coupons = array(); 
            foreach($data as $row)
            {    
                $scheme =  $row['scheme_name'];
                $book =  $row['book_no2'];
                $coupons[] = $row['coupon']; 
                $cit =  $row['city'];
                $amou =  $row['amount'];
                $rec =  $row['receipt_no']; 
            } 
if(!strlen($result) >0 || $result==0)
    {   
        $errors['Message']= "Data Not Found";
    }

    }
    ?>

PDO::fetchAll will return an array of records; meaning an empty array if there are no found records. You can check if an array is empty by using count/sizeof

<?php
if ($_POST && isset($_POST['submit'])) {
    $result = $db->prepare('SELECT scheme_name,city,coupon,amount,receipt_no,book_no2 FROM scheme_master WHERE receipt_no=:receipt_no');
    $result->bindParam(':receipt_no', $_POST['receipt_no']);
    $result->execute();
    $data    = $result->fetchAll();
    $coupons = array();

    if (count($data) == 0) {
        $errors['Message'] = "There are no results";
    } else {
        foreach ($data as $row) {
            $scheme    = $row['scheme_name'];
            $book      = $row['book_no2'];
            $coupons[] = $row['coupon'];
            $cit       = $row['city'];
            $amou      = $row['amount'];
            $rec       = $row['receipt_no'];
        }
    }
}
?>