abstract class BaseModel
{
protected $db;
protected $table;
protected $primaryKey = 'id';
public function __construct()
{
try {
$this->db = new PDO("mysql:dbname=7todo;host=localhost", 'root', '');
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
public function find($id){
$sql = "select * from {$this->table} where {$this->primaryKey} = :id";
$stmt = $this->db->prepare($sql);
$stmt->execute([':id' => $id]);
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function update($data){
# code for update a model
}
public function create($data){
# code for create a model
}
public function delete($id){
$sql = "delete from {$this->table} where {$this->primaryKey} = :id";
$stmt = $this->db->prepare($sql);
$stmt->execute([':id' => $id]);
return $stmt->rowCount();
}
}
Elias Alipour ۲۱ تیر ۱۴۰۲، ۰۹:۲۷
همون کدای خود اموزشه فقط دیتابیس جدید ساختم
PS C:\\xampp\\htdocs\\7Learn-PHP-Advanced-Chapter\\oop-4-principles\\Inheritance> php user.php
Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it
خطای بالا به هنگام ران شدن تو ترمینال
Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table '7todo.users' doesn't exist in C:\\xampp\\htdocs\\7Learn-PHP-Advanced-Chapter\\oop-4-principles\\Inheritance\\BaseModel.php:19