王兆盟homework6

本次作业要求用PDO模板实现IMode中的四个方法,并且用参数绑定的方法防止sql注入。

";
    $dbh = null;
}
catch (PDOException $e)
{
    die ("错误: " . $e->getMessage() . "
"); } interface iModel { public function select(string $statements,array $params = []); public function update(string $statement); public function delete(string $statement); public function insert(string $statement); } class PDOmodeL implements IModel { protected $dbh; public function __construct($dsn, $user, $pass) { $this->dbh = new PDO($dsn, $user, $pass); } public function select(string $statements,array $params = []) { $result = $this->dbh->prepare($statements); if($params){ foreach($params as $i=>$v){ $result->bindParam($i,$v); } $res = $result->fetch(PDO::FETCH_ASSOC); var_dump($res); } $count= $result->execute(); return $count; } public function update(string $statements,array $params = []) { $result = $this->dbh->prepare($statements); if($params){ foreach($params as $i=>$v){ $result->bindParam($i,$v); } } $count= $result->execute(); return $count; } public function delete(string $statements,array $params = []) { $result = $this->dbh->prepare($statements); if($params){ foreach($params as $i=>$v){ $result->bindParam($i,$v); } } $count= $result->execute(); return $count; } public function insert(string $statements,array $params = []) { $result = $this->dbh->prepare($statements); if($params){ foreach($params as $i=>$v){ $result->bindParam($i,$v); } } $count= $result->execute(); return $count; } } ?>

你可能感兴趣的:(王兆盟homework6)