简体   繁体   中英

php mysql looking how to check if there are no result

I have this code

require("connession.php");
$contabilita="d";
$dataa="10/12/2018";
$datam_0=explode("/", $dataa);
$datam = "".$datam_0[2];
$result = mysqli_query($con,"SELECT Max(n_ricevuta)+1 as max_ricevuta 
                            FROM corrispettivi_mod 
                            where anno='$datam' 
                            and contabilita='$contabilita'");
$row = mysqli_fetch_assoc($result);
if(mysqli_num_rows($result) > 0){
echo $row['max_ricevuta'];
}
else{echo "1";}

here is my db

data        anno    n_ricevuta  contabilita
2019-12-01  2019    1           nd

in my db I have only a row with a value of the filed 'anno' 2019 so it must print '1' but it doesn't.

if I modify in the db the field anno with the value "2018", it works.

help me thanks

When using an aggregate function like max() you will always get a row, if there isn't a matching row it will return null .

So you need to change your test to...

if($row['max_ricevuta'] != null){
    echo $row['max_ricevuta'];
}
else {
    echo "1";
}

As RiggsFolly also points out, worth checking the values your using in your testing, you have

$contabilita="d";

whereas the example data shows nd .

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