Checking to see if record exist by RecordCount()

101 Views Asked by At

I have this function in a while loop and for some reason it says all records are duplicates.

I have tried:

$tst->RecordCount() === 1
$tst->RecordCount() == 1
$tst->RecordCount() = 1

$tst->RecordCount() === '1'
$tst->RecordCount() == '1'
$tst->RecordCount() = '1'

$tst->RecordCount() === "1"
$tst->RecordCount() == "1"
$tst->RecordCount() = "1"

But none seem to work

        $sql45 = "SELECT 
                count(*)
            FROM
                companies 
            WHERE 
                MC = " . $test;
    $tst = $conn->Execute($sql45);
    if ($tst === false) die("horrible death:" . $conn->ErrorMsg() . " SQL: " . $sql45); 

   if($tst->RecordCount() === 1){
        echo $test . "<br>";
        echo "Duplicate record <br>";
        continue;

    } 

this is what i recieve

545481
Duplicate record 
45
Duplicate record 
11111
2

There are 2 best solutions below

0
On

Your record count is ALWAYS going to be 1. You're have

SELECT count(*) FROM companies WHERE MC = somevalue

This always returns a single row, aka a record count of 1. Perhaps it is the VALUE of the count that you are trying to get?

2
On

You can check a row exists or not by using this:

SELECT EXISTS(SELECT 1 FROM table1 WHERE ...)

It is more efficient than count(*).Hope it helps.