• PHP如何连接MySQL数据库
  • 发布于 2个月前
  • 394 热度
    0 评论

前言:

连接数据库是我们开发管理信息系统最基本的一个操作,但是对于很多PHP的初学者来说,如何连接数据库还是一个未解之谜。基于对PHP初学者的关爱,我这边以PHP连接mysql数据库为例,总结汇总了一个PHP连接mysql数据库的操作类,涵盖了常规的数据库连接,查询,回滚等常见操作,希望对于PHP的初学者有所帮助。

PHP连接MySQL代码:

<!--?php
class mysql {
    //堆代码 duidaima.com
    private $defaultDB = null;
    private $link = null;
    private $sql = null;
    private $bindValue = null;
    public $num_rows = 0;
    public $affected_rows = 0;
    public $insert_id = 0;
    public $queries = 0;
 
    public function __construct() {
 
        if(func_num_args()) {
            $argv = func_get_arg(0);
            if(!empty($argv) && is_array($argv)) { 
                $this--->connect($argv);
                $argv['charset'] = isset($argv['charset']) ? $argv['charset'] : 'utf8';
                $this->setCharset($argv['charset']);
            }
        }
 
    }
 
    public function connect($argv, $charset = null) {
 
        if($this->link) return false;
        $argv = func_get_arg(0);
        $argv['port'] = isset($argv['port']) ? $argv['port'] : 3306;
        $this->link = mysqli_connect( $argv['host'], $argv['user'], $argv['password'], $argv['database'], $argv['port']);
        if(mysqli_connect_errno()) { echo mysqli_connect_error(); exit(0); }
        $this->defaultDB = $argv['database'];
 
        if($charset)
        $this->setCharset($charset);
    }
 
    public function selectDB($database){
 
        $int = mysqli_select_db($this->link, $database);
        if($int) $this->defaultDB = $database;
        return $int;
 
    }
 
    public function query($sql) {
 
        $result = mysqli_query($this->link, $sql);
        if(mysqli_errno($this->link)) { echo mysqli_error($this->link); exit(0); }
 
        $this->queries++;
 
        if(preg_match('/^use\\s+(\\w+)/', $sql, $matches)) list($range, $this->defaultDB) = $matches;
 
        $pattern = array('read'=> '/(?<=select|show)(.+)$/i', 'write'=> '/(?<=alter|use|replace|insert|update|delete)(.+)$/i');
        if(preg_match($pattern['write'], $sql)) {
            $this->affected_rows = mysqli_affected_rows($this->link);
        }else{
            $this->num_rows = mysqli_num_rows($result);
        }
 
        if(preg_match('/^insert(.+)$/i', $sql)) {
            $this->insert_id = mysqli_insert_id($this->link);
        }
 
        return $result;
 
    }
 
    public function find($sql) {
 
        $collection = array();
        $result = $this->query($sql);
        while($rows = mysqli_fetch_assoc($result))
        array_push($collection, $rows);
        mysqli_free_result($result);
        return $collection;
 
    }
 
    public function findOne($sql) {
 
        $result = $this->query($sql);
        $rows = mysqli_fetch_assoc($result);
        mysqli_free_result($result);
        return $rows;
 
    }
 
    public function setCharset($charset) {
 
        return mysqli_set_charset($this->link, $charset);
 
    }
 
    public function prepare($sql) {
 
        $this->sql = $sql;
 
    }
 
    public function bindValue($search, $value) {
 
        $this->bindValue = array();
        $this->bindValue[$search] = $value;
 
    }
 
    public function execute() {
 
        if(func_num_args()) {
            $argv = func_get_arg(0);
            if(!empty($argv) && is_array($argv)) {
                if(!is_array($this->bindValue)) $this->bindValue = array();
                $this->bindValue = array_merge($this->bindValue, $argv);
            }
        }
 
        if($this->bindValue) {
            foreach($this->bindValue as $search => $value) {
                $this->sql = str_replace($search, $this->escape($value), $this->sql);
            }
            $this->bindValue = null;
        }
 
        $int = $this->query($this->sql);
        //$this->sql = null;
        return (boolean) $int;
    }
 
    public function escape($string) {
 
        return mysqli_real_escape_string($this->link, $string);
 
    }
 
    public function close() {
 
        return mysqli_close($this->link);
 
    }
 
    public function ping() {
 
        return mysqli_ping($this->link);
 
    }
 
    public function autoCommit($boolean) {
 
        return mysqli_autocommit($this->link, $boolean);
 
    }
 
    public function commit() {
 
        return mysqli_commit($this->link);
 
    }
 
    public function rollback() {
 
        return mysqli_rollback($this->link);
 
    }
 
    public function __destruct() {
 
        if($this->link) $this->close();
        unset($this->link, $this->defaultDB, $this->bindValue, $this->sql, $this->result, $this->num_rows, $this->affected_rows, $this->insert_id);
 
    }
 
}
 
$argv = array(
'host' => 'localhost',
'user' => 'root',
'password' => '',
'port' => 3306,
'database' => 'test',
'charset'=> 'utf8');
 
// Using the "mysql::__construct" method to connect MySQL database
 
$mysql = new mysql($argv);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);
 
// Using the "mysql::connect" method to connect MySQL database
 
$mysql = new mysql();
$mysql->connect($argv);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);
 
$mysql = new mysql();
$mysql->connect($argv);
$mysql->setCharset($argv['charset']);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);

总结:

以上就是我们汇总的PHP连接MySQL数据库的常见操作,包括数据库的连接,查询,及连接字符串的配置等常见操作,希望对你有所帮助。

用户评论