I'm trying to create an OOP style CRUD class in PHP and using PDO prepared statements to protect against injections. My connection is working and I can perform regular SQL queries from the class but when I try to incorporate PDO's prepare function, I get an error that I either have a MySQL syntax error or PDO prepare is undefined.
The error gets thrown at $p_query = $db->prepare($sql)
line. Can anyone spot what I am doing wrong?
<?php
require_once 'dbconfig.php';
class Crud {
protected $db;
private static function fetchQuery($sql, $values) {
echo $sql;
var_dump($values);
$db = Db_conn::pdoBuilder();
$p_query = $db->prepare($sql);
$p_query->execute($values);
$results = $p_query->fetch(PDO::FETCH_OBJ);
return $results;
}
public static function show($tbl, $id) {
$sql = '"SELECT * FROM (:tbl) WHERE id = (:id)"';
$values = [':tbl' => $tbl, ':id' => $id];
$results = self::fetchQuery($sql, $values);
return $results;
}
public static function listAll($tbl) {
$sql = '"SELECT * FROM (:tbl)"';
$values = [':tbl' => $tbl];
$results = self::fetchQuery($sql, $values);
return $results;
}
}
$sql
and$values
– Marco Aurélio DeleuVALUES
(in case of an insert)\ – Elias Van Ootegem