<?php
class Conmysql{
public $servername;
public $username;
public $password;
public $dbname;
public $con=null;
public function __construct($servername,$username,$password,$dbname){
$this->servername=$servername;
$this->username=$username;
$this->password=$password;
$this->dbname=$dbname;
}
public function getConnection(){
$dsn="mysql:host=".$this->servername.";dbname=".$this->dbname;
try {
$this->con = new PDO($dsn, $this->username, $this->password); //初始化一个PDO对象
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "<br/>");
}
}
public function updateData($sql){
if($this->con==null){
$this->getConnection();
}
$res=$this->con->exec($sql);
$arr=array("result"=>$res);
echo json_encode($arr);
$this->closeCon();
}
public function closeCon(){
$this->con=null;
}
}
class realConn extends Conmysql{
public function __construct($servername,$username,$password,$dbname){
parent::__construct($servername,$username,$password,$dbname);
}
public function realUpdate(){
$sql="UPDATE `praise` SET `num`=num+1 WHERE `id`=1";
$this->updateData($sql);
}
}
$praiseCon=new realConn('localhost','root','','phptest');
$praiseCon->realUpdate();
?>
网友评论