当前位置 : 主页 > 网络编程 > PHP >

SQL_adapter.php

来源:互联网 收集:自由互联 发布时间:2021-06-28
SQL_adapter.php connect_error) {//die("The connection fails". $conn-connect_error);//}//echo "The connection is successful"; private $db_host; //数据库主机 private $db_user; //数据库登陆名 private $db_pwd; //数据库登陆密码
SQL_adapter.php
 connect_error) {
//die("The connection fails". $conn->connect_error);
//}
//echo "The connection is successful";
    private $db_host;       //数据库主机
    private $db_user;       //数据库登陆名
    private $db_pwd;        //数据库登陆密码
    private $db_name;       //数据库名
    private $db_charset;    //数据库字符编码
    private $db_pconn;      //长连接标识位
    private $debug;         //调试开启
    private $conn;          //数据库连接标识
    private $msg = "";      //数据库操纵信息

    public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
        $this->db_host = $db_host;
        $this->db_user = $db_user;
        $this->db_pwd = $db_pwd;
        $this->db_name = $db_name;
        $this->db_charset = $db_chaeset;
        $this->db_pconn = $db_pconn;
        $this->result = null;
        $this->debug = $debug;
        $this->initConnect();
    }
    public function initConnect() {

            $this->conn = @mysqli_connect($this->db_host, $this->db_user, $this->db_pwd);

        if ($this->conn) {
            $this->query("SET NAMES " . $this->db_charset);
        } else {
            $this->msg = "数据库连接出错,错误编号:" . mysqli_connect_error() . "错误原因:" . mysqli_connect_error();
        }
        $this->selectDb($this->db_name);
    }
    public function selectDb($dbname) {
        if ($dbname == "") {
            $this->db_name = $dbname;
        }
        if (!mysqli_select_db($this->conn, $this->db_name)) {
            $this->msg = "数据库不可用";
        }
    }
    //创建表
    public function creatTable($tableName,$colNumber){
        $sql = "CREATE " . "TABLE " . $tableName . " (" ." id int NOT NULL AUTO_INCREMENT,";
        $colstring="";
        $ii=1;
        for($i=0;$i<$colNumber;$i++)
        {

                $colstring=$colstring . "a" . $ii ." varchar(255),";


            $ii++;
        }
        $sql=$sql . $colstring ." PRIMARY KEY(id)" . ")";
        $this->query($sql);
    }
    //------------------------
    public function query($sql, $debug = false) {
        if (!$debug) {
            $this->result = mysqli_query($this->conn, $sql);
        } else {

        }
        if ($this->result == false) {
            $this->msg = "sql执行出错,错误编号:" . $this->result . "错误原因:" . mysqli_error($this->conn);
        }
        //        var_dump($this->result);
    }

    public function select($tableName, $columnName = "*", $where = "") {
        $sql = "SELECT " . $columnName . " FROM " . $tableName;
        $sql .= $where ? " WHERE " . $where : null;
        $this->query($sql);
    }

    public function findAll($tableName) {
        $sql = "SELECT" ." *  FROM " . $tableName;
        $this->query($sql);
    }

    public function insert($tableName, $column = array()) {
        $columnName = "";
        $columnValue = "";
        foreach ($column as $key => $value) {
            $columnName .= $key . ",";
            $columnValue .= "'" . $value . "',";
        }
        $columnName = substr($columnName, 0, strlen($columnName) - 1);
        $columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
        $sql = "INSERT"." INTO " .  $tableName . "(" . $columnName . ")"  . " VALUES " . "(" . $columnValue . ")";
        $this->query($sql);
        if($this->result){
            $this->msg = "数据插入成功。新插入的id为:" . mysqli_insert_id($this->conn);
        }
    }

    public function update($tableName, $column = array(), $wherestr = "") {
        $updateValue = "";
        foreach ($column as $key => $value) {
            $updateValue .= $key . "='" . $value . "',";
        }
        $updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
        $sql = "UPDATE " . $tableName . " SET " . $updateValue;
        $sql .= $wherestr ? "  WHERE  " .$wherestr : null;
        $this->query($sql);
        if($this->result){
            $this->msg = "数据更新成功。受影响行数:" . mysqli_affected_rows($this->conn);
        }
    }

    public function delete($tableName, $where = ""){


       $sql = "DELETE FROM " . $tableName;
        $sql .= $where ? " WHERE " .$where : null;

       $this->query($sql);
      if($this->result){
           $this->msg = "数据删除成功。受影响行数:" . mysqli_affected_rows($this->conn);
          return $this->msg;
       }

    }

    public function fetchArray($result_type = MYSQLI_BOTH){
        $resultArray = array();
        $i = 0;
        while($result = mysqli_fetch_array($this->result, $result_type)){
            $resultArray[$i] = $result;
            $i++;
        }
        return $resultArray;
    }

    //    public function fetchObject(){
    //        return mysql_fetch_object($this->result);
    //    }

    public function printMessage(){
        return $this->msg;
    }

    public function freeResult(){
        @mysqli_free_result($this->result);
    }

    public function __destruct() {
        if(!empty($this->result)){
            $this->freeResult();
        }
        mysqli_close($this->conn);
    }
}


/*简单使用*/
//操作数据库阶段
	$SQL_adapter->query('select * from ljst_user where id=40');
    $res = $SQL_adapter->fetchArray(MYSQLI_ASSOC);
    //var_dump($res);
上一篇:Crawling.php
下一篇:api.php
网友评论