4

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'].

The value will be true or 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?

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($clldsq, $clldtm, $tdcode);
return $res;
}
Fayyaz Ali
  • 787
  • 1
  • 8
  • 18

5 Answers5

2

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

Thanks.

vidura
  • 65
  • 1
  • 1
  • 7
0

With select * , you're querying the complete table contents.

Try using select count(*) from , and check the return value.

donfede
  • 734
  • 1
  • 9
  • 25
0

Please change the condition to this....

if(count($row)>0 && $row[4] != ""){
                $res=$row[4];
                //print_r($row);
        }  
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6
0

I think you need to see this post: MySQL SELECT only not null values

Introduce a second condition to check if the values are not null.

Community
  • 1
  • 1
Zaheer Ahmad Awan
  • 122
  • 1
  • 1
  • 9
0
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.

Fayyaz Ali
  • 787
  • 1
  • 8
  • 18