點(diǎn)贊加一功能

<?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); //初始化一個(gè)PDO對(duì)象
            } 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();
?>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。