sqlite: How can I check with PHP if a Record already exists?

Viewed 69

I want to use PHP to check if a database entry (SQLlight) already exists.

I fetch the DB Records first:

$db->exec("SELECT * FROM $tbMgmt WHERE resourceId LIKE '$resourceId' AND EventType Like '$EventType'");

When the Result is 0, I want to proceed with my Script. if the Result is 1 or higher, I want to stop:

if ($db) {

    echo "Record already exists!";
} else {

 // proceed with my Script ...
}

How can I solve this?

1 Answers

You can count the number of rows returned from your query;

$result = $db->query("SELECT * FROM $tbMgmt WHERE resourceId LIKE '$resourceId' AND EventType Like '$EventType'");
$count = $result->rowCount(); 

if($count == 0){
    // proceed with Script ...
}
Related