Search code examples
phpsqlitepdoprepared-statementprepare

SELECT WHERE with PDO prepare in PHP


I create this table:

CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Username TEXT UNIQUE NOT NULL, Password TEXT NOT NULL, Time INTEGER UNIQUE NOT NULL, Timezone INTEGER NOT NULL, Active BOOLEAN NOT NULL);

and fill it with several rows. Then I'm trying to SELECT the 'Active' for a specific 'Username'. This is just one cell.

I want to do this with a prepared statement, but the problem is I can't even do it with a normal statement. This thing fails:

SELECT Active FROM Users WHERE Username "John"

This works though:

SELECT Active FROM Users WHERE Username LIKE "John%"

But I'm looking for an exact result. This one works too:

SELECT Active FROM Users WHERE Id=1

What am I missing?

EDIT

There is indeed a typo in my statement. A "=" is required (I'm leaving it as it is above). However this wasn't really the problem. I've traced it back in the use of prepared statements.

Whenever I store someting in the db using prepared statements, I can't search it using this:

SELECT Active FROM Users WHERE Username="John"

I'm very curious to see why this is happening but it belongs to another question.


Solution

  • SELECT Active FROM Users WHERE Username="John"
    

    Notice "="