简体   繁体   English

php mysql寻找如何检查是否没有结果

[英]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. 在我的数据库中,我只有一行的字段值为``anno''2019,因此它必须打印为``1'',但没有。

if I modify in the db the field anno with the value "2018", it works. 如果我在数据库中修改字段anno的值为“ 2018”,则它可以工作。

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 . 当使用像max()这样的聚合函数时,您总是会得到一行,如果没有匹配的行,它将返回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 正如RiggsFolly指出的那样,值得检查您在测试中使用的值,

$contabilita="d";

whereas the example data shows nd . 而示例数据显示nd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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