Reputation: 107
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
Reputation: 22532
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