Запрос:
public function query($sql, $params = []) {
$stmt = $this->db->prepare($sql);
if (!empty($params)) {
foreach ($params as $key => $val) {
if (is_int($val)) {
$type = PDO::PARAM_INT;
} else {
$type = PDO::PARAM_STR;
}
$stmt->bindValue(':'.$key, $val, $type);
}
}
$stmt->execute();
return $stmt;
}
Вызов:
$params = [
'id' => '',
'name' => $post['name'],
'description' => $post['description'],
];
$this->db->query('INSERT INTO events VALUES (:id, :name, :description)', $params); // print_r
// PDOStatement Object
// (
// [queryString] => INSERT INTO events VALUES (:id, :name, :description)
// )
return $this->db->lastInsertId();
Этот код работает:
public function row($sql, $params = []) {
$result = $this->query($sql, $params); // query тот же
return $result->fetchAll(PDO::FETCH_ASSOC);
}
return $this->db->row('SELECT * FROM events ORDER BY id DESC LIMIT :start, :max', $params);
Версия php: PHP/7.1.11