CKLAB NIF
CKLAB NIF

Reputation: 107

Placing a variable into a PDO Statement

is it possible to create a pdo query with a variable? Example:

$q = "SELECT COUNT (*) c FROM blogpages WHERE keywords LIKE '%test%' ";

then

$query = $db->query("$q");

$result = $query->fetch(PDO::FETCH_ASSOC);

when i do this i get an error

"Call to a member function fetch() on a non-object in C....."

i want to know is there a way to place the query in there as a variable because the query changes depending on how many OR statements are in the query

Upvotes: 2

Views: 97

Answers (1)

Saty
Saty

Reputation: 22532

query()

PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object

And problem in your query with space between count and (*)

SELECT COUNT (*)..
            ^^

SO no need to fetch data just use

$q = "SELECT COUNT(*) c FROM blogpages WHERE keywords LIKE '%test%' ";
foreach ($db->query($q) as $row) {
        print $row['c'] . "\t";
    }

Upvotes: 4

Related Questions