I am trying to use an update method inside my Database class where I could be able to update a record. I am trying to make sure that I could use the method in another instance where i dont have to repeat writing the same statement. Here is my code:
<?php
require 'init.php';
class Database {
private $conn;
public function __construct() {
try {
$this->conn = new PDO('mysql:host=localhost;dbname=school', DB_USER, DB_PASS);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
return false;
}
}
public function update($table, $key, $value, $id) {
$stmt = $this->conn->prepare("UPDATE $table SET $key = $value WHERE id = :id");
return $stmt->execute(array($key => $value, 'id' => $id));
}
}
$database = new Database();
My problem is i get some errors when i try to instantiate the class $result = $database->update('admin', 'username', 'golobo', 13);
the question is what am I doing wrong?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…