码迷,mamicode.com
首页 > 数据库 > 详细

[php]mysqli操作流程

时间:2015-02-07 14:28:32      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

<?php

class SqlTools{
    private $con;
    private $trans;
    public function __construct($host, $user, $pswd, $db){
        $this->con = new mysqli($host, $user, $pswd, $db);
        if($this->con->connect_error)
            die("CONNECT ERROR:".$this->con->connect_errno.":".$this->con->connect_error."<br/>");
        $this->con->query("set names utf8");
    }
    /*start transaction*/
    public function start_trans(){
        $this->con->autocommit(false);
        $this->trans = true;
    }
    /*rollback*/
    public function rollback(){
        $this->con->rollback();
    }
    /*add, insert, update and so on*/
    public function execute($sql){
        $exec_bool = $this->con->query($sql);
        if($exec_bool)
            die("EXEC ERROR:".$this->con->errno.":".$this->con->error."<br/>");
        return $this->con->affected_rows;
    }
    /*select*/
    public function &execute_query($sql){
        $res = $this->con->query($sql);
        if(!$res)
            die("QUERY ERROR:".$this->con->errno.":".$this->con->error."<br/>");
        return $res;
    }
    /*free resource*/
    public function close(){
        if($this->trans)
            $this->rollback();
        if($this->con)
            $this->con->close();
    }
}

 

[php]mysqli操作流程

标签:

原文地址:http://www.cnblogs.com/fantasy01/p/4278741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!