0

I am trying to verify that my query is grabbing at least 1 row from the database.

$db = new SQLite3('../portfolio.db');

$results = $db->prepare('SELECT COUNT(*) FROM users WHERE id = :id');
$results->bindValue(':id', 7);

$grab = $results->execute();

if([COUNT(*) VALUE] == 1) {
    echo "true";
}

What I'm trying to figure out is what could I replace [COUNT(*) VALUE] with in the if statement to echo out "true".

Anyone know how this is done? I assume there's a way to return the number of COUNT(*) but I'm not entirely sure.

The question similar to mine didn't work most likely because he/she is using regular mysql/php whereas this is PHP PDO and SQLite3.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87

1 Answers1

2

use an alias in query so

$db = new SQLite3('../portfolio.db');

$results = $db->prepare('SELECT COUNT(*) as count FROM users WHERE id = :id');
$results->bindValue(':id', 7);

$results->execute();//execute method just return true or false so no need to use variable here

if($results->fetch()['count'] == 1) {//you need to fetch() first row and column name (in this case is alias "count")
    echo "true";
}
rafwlaz
  • 484
  • 2
  • 17