user712410
user712410

Reputation:

PDO SQLite difficulties

Could someone please tell me what is wrong with my code. When I try to use the function, I get:

Fatal error: Call to a member function setFetchMode() on a non-object in ...

Using:

if (!$IDq) { print_r($dbh->errorInfo()); } 

gives me: file is encrypted or is not a database

I have been messing with it for ages but can't seem to get it to work.

function user_registered($user_id){
$dbh = new PDO("sqlite:datastore/userids.db");
$IDq = $dbh->query("SELECT * FROM users_identify WHERE identifier = '$user_id'");
$IDq->setFetchMode(PDO::FETCH_ASSOC);
$IDf = $IDq->fetch();
if($IDf['id']){
return $IDf['id'];
}else{
return 0;
}
}

Thanks.

EDIT: Ah, thanks for your help. I found the problem, it was that I was using an sqlite 2 database when I needed an sqlite 3 database.

Upvotes: 0

Views: 1579

Answers (3)

user712410
user712410

Reputation:

I found the problem, it was that I was using an sqlite 2 database when I needed an sqlite 3 database. I guess that was the source of the 'file is encrypted or is not a database' message.

var_dump($dbh->errorInfo());

Helped my find the source of the problem. :)

Upvotes: 0

Deltik
Deltik

Reputation: 1137

By default, PDO will not return a PDOStatement from PDO::query() if the query failed. Instead, $IDq would be set to false, which is a non-object.

Something is wrong with your query or PDO object, and you can check it by:

var_dump($dbh->errorInfo());

Also, PHP advises that if you are going to use PDO::query() that you protect your query this way:

$IDq = $dbh->query("SELECT * FROM users_identify WHERE identifier = " . $dbh->quote($user_id));

Upvotes: 1

Radek
Radek

Reputation: 8376

I would say, that you have an error in your SQL query and line

$dbh->query('...')

returns false - according to manual. Then you can't call method setFetchMode on false.

Enable throwing exceptions and it should help you:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Upvotes: 1

Related Questions