点赞加一功能

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() . "
"); } } 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(); ?>

你可能感兴趣的:(点赞加一功能)