简体   繁体   English

检查mysql表php中是否存在任何记录

[英]checking if any record exists in mysql table php

I have created the function below; 我创建了以下功能;

To check if a record exists in the db or not, if record exists should return true, else false and currently returns the fourth column of the row which is the field $row['isannounced']. 要检查数据库中是否存在记录,如果存在记录,则返回true,否则返回false,并且当前返回行的第四列,即$ row ['isannounced']字段。

The value will be true or false. 该值将为true或false。 But the problem is if the row is empty, the row count will still be 1. is there a better way to handle this? 但问题是如果行是空的,行数仍然是1.有没有更好的方法来处理这个?

Thanks in advance. 提前致谢。

function isnotificationannounced($dspid, $clldsq, $clldtm){
//echo 'in : isnotificationannounced<br>';

$res=false;
$qry = "SELECT * FROM tbl_maindisplay_notification where (clldsq='$clldsq' AND clldtm='$clldtm' AND dspid='$dspid')";
//echo $qry;
    $result = querydb($qry);
    if ($result) {
        $row = mysql_fetch_array($result);
        //echo 'row data:<br>';print_r($row);
        if(count($row)>0){
                $res=$row[4];
                //print_r($row);
        }           
    } else {
        die("existsindb: Query failed");
    }
    unset($clldsd, $clldtm, $tdcode);
return $res;
}

use mysql_num_rows($result) insted of count($result) 使用mysql_num_rows($ result)insted count($ result)

Thanks. 谢谢。

With select * , you're querying the complete table contents. 使用select *,您将查询完整的表格内容。

Try using select count(*) from , and check the return value. 尝试使用select count(*)from,并检查返回值。

Please change the condition to this.... 请将条件更改为....

if(count($row)>0 && $row[4] != ""){
                $res=$row[4];
                //print_r($row);
        }  

I think you need to see this post: MySQL SELECT only not null values 我想你需要看到这篇文章: MySQL SELECT只是不是空值

Introduce a second condition to check if the values are not null. 引入第二个条件来检查值是否为空。

function isnotificationannounced($dspid, $clldsq, $clldtm){
//echo 'in : isnotificationannounced<br>';

$res=false;
$qry = "SELECT * FROM tbl_maindisplay_notification where (clldsq='$clldsq' AND clldtm='$clldtm' AND dspid='$dspid')";
//echo $qry;
    $result = querydb($qry);
    if ($result) {
        $row = mysql_fetch_array($result);
        //echo 'row data:<br>';print_r($row);
        if(empty($row)){
            $res=true;
        }else{
                $res=$row[4];
                //print_r($row);
        }

    } else {
        die("existsindb: Query failed");
    }
    unset($clldsd, $clldtm, $tdcode);
return $res;
}

Thanks to all of you.. 感谢大家..

I have updated the code to above, if you have any recommendations/suggestions, please let me know. 我已将代码更新到上面,如果您有任何建议/建议,请告诉我。

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

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