zoukankan      html  css  js  c++  java
  • MYSQL工具类简单实现

     1 <?php
     2 /*
     3     数据库操作工具类
     4 */
     5 
     6     class MySQLDB{
     7         //定义数据库属性信息
     8         private $host;
     9         private $port;
    10         private $user;
    11         private $password;
    12         private $charset;
    13         private $dbname;
    14         //资源连接
    15         private $link;
    16 
    17         //定义构造方法
    18         public function __construct($config = array()){
    19             //初始化数据信息
    20             $this->initServer($config);
    21 
    22             //连接数据库操作
    23             $this->connectServer();
    24 
    25             //设置字符集
    26             $this->setCharset();
    27 
    28             //设置数据库
    29             $this->selectDB();
    30         }
    31 
    32         /**
    33             数据格式
    34             $config = array(
    35                 'host'=>'localhost',
    36                 'port'=>'3306',
    37                 'user'=>'root',
    38                 'password'=>'root',
    39                 'charset'=>'UTF8',
    40                 'dbname'=>'test'
    41             );
    42 
    43         */
    44         //初始性信息配置
    45         private function initServer($config){
    46 
    47             //给定默认初始化值
    48             $this->host=isset($config['host']) ? $config['host'] : 'localhost';
    49             $this->port=isset($config['port']) ? $config['port'] : '3306';
    50             $this->user=isset($config['user']) ? $config['user'] : '';
    51             $this->password=isset($config['password']) ? $config['password'] : '';
    52             $this->charset=isset($config['charset']) ? $config['charset'] : 'UTF8';
    53             $this->dbname=isset($config['dbname']) ? $config['dbname'] : 'test';
    54         }
    55 
    56         //连接函数
    57         private function connectServer(){
    58 
    59             $res = mysql_connect("$this->host:$this->port","$this->user","$this->password");
    60             $this->link=$res;
    61             //判断数据库连接是否成功
    62             if (!$this->link) {  //资源型 转换 数据 是 true | false
    63                 die("数据库连接失败".mysql_error());
    64             }else{
    65                 echo "数据库连接成功!";
    66             }
    67         }
    68 
    69         //设置字符集编码
    70         private function setCharset(){
    71             $sql="set names `$this->charset`";
    72             $this->query($sql,$this->link);  //调用自己的query()方法
    73         }
    74 
    75         //选择默认数据库
    76         private function selectDB(){
    77             $sql="use `$this->dbname`";
    78             $this->query($sql);  //调用自己的query()方法
    79         }
    80 
    81         //sql执行
    82         public function query($sql){
    83             //执行sql语句
    84             $result = mysql_query($sql,$this->link);
    85             //执行成功,return true,执行带返回结果 属于mixed类型
    86             if ($result == false) {
    87                 echo "SQL查询失败:".mysql_error().die();
    88             }else{
    89                 //执行成功 
    90                 return $result;
    91             }
    92         }
    93     }
     1 //自动加载机制
     2 function __autoload($class_name){
     3     require './'.$class_name.'class.php';  //自动加载需要 未定义的 类文件。
     4 }
     5 
     6 
     7     // myAutoload('MySQLDB'); //加载mysql类*/
     8 
     9     require './MySQLDB.class.php';
    10     //用户配置信息
    11     $config=array(
    12         'host'=>'localhost',
    13         'port'=>'3306',
    14         'user'=>'root',
    15         'password'=>'root',
    16         'charset'=>'utf8',
    17         'dbname'=>'mytest'
    18         );
    19 
    20     //数据库实例
    21     $dao = new MySQLDB($config);
    22     var_dump($dao);
  • 相关阅读:
    hdoj 2063 过山车
    hdoj 2112 HDU Today
    hdoj 1874 畅通工程续
    hdoj 2544 最短路
    sound of the genuine
    复习webpack的常用loader
    node-sass安装报错
    react-debug
    react-router 4v 路由嵌套问题
    React 中使用sass
  • 原文地址:https://www.cnblogs.com/sharecorner/p/6122987.html
Copyright © 2011-2022 走看看