美文网首页
点赞加一功能

点赞加一功能

作者: 扶搏森 | 来源:发表于2017-12-11 17:11 被阅读0次
    <?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();
    ?>
    

    相关文章

      网友评论

          本文标题:点赞加一功能

          本文链接:https://www.haomeiwen.com/subject/gfxwixtx.html