Mysql数据库实用工具类--含单例模块

继前两章的数据库工具类不断完善,现在加进去单例模式,代码都是在我这边运行好的,可以直接拿去运行。

这些工具类都是特别基础的,建议好好看看,对于初学者学习后面的pdo和封装会有特别好的效果作用。

<?php
header('content-type:text/html;charset=utf-8');
error_reporting(E_ALL ^ E_DEPRECATED);
$config=array(
        'host'=>"localhost",
    'port'=>3306,
    'user'=>"root",
    'pass'=>'',
    'char'=>"utf8",
    'db'=>"test"
    );

class MYSQL{

  private  $link=null;
    private $host;
    private $port;
    private $user;
    private $pass;
    private $char;
    private $db;

// 添加单例模块
private static $instance=null;



    static function GetInstance($config){  // 获取对象
        $obj=new self($config);
        if(!isset(self::$instance))
        {
            self::$instance=new self($config); //判断如果instance为空则新建一个对象 然后存到静态属性instance里面
        } return self::$instance;   //如果静态属性instance中已经有了对象则直接返回即可
    }
private function __clone(){

} // 私有化克隆方法,防止外界通过克隆手段来重新生成对象
// 单例模块结束

private  function __construct($config){ 
        $this->host=!empty($config['host']) ? $config['host'] : "localhost";
        $this->port=!empty($config['port']) ? $config['port'] : "3306";
        $this->user=!empty($config['user']) ? $config['user'] : "root";
        $this->pass=!empty($config['pass']) ? $config['pass'] : "";
        $this->char=!empty($config['char']) ? $config['char'] : "utf8";
        $this->db=!empty($config['db']) ? $config['db'] : "test";
      $link=@mysql_connect("{$config['host']}:{$config['port']}","{$config['user']}","{$config['pass']}");
       $this->charset($this->char);
       $this->usedb($this->db);
  }

  function charset($charset){  // 设置字符编码
    mysql_query("set names {$charset} ");
  }

  function usedb($db){  //更改要使用的数据库
     mysql_query("use {$db}");
     echo "已切换数据库 {$db} <br>";
  }

  function closeDB(){  //关闭数据库
      mysql_close($this->link);
      echo "已关闭数据库连接";
  }

  function exec($sql){ // 返回增,删,改语句的返回值为真假boolen
       $result=$this->query($sql);
       return true; //因为是增删改语句,所以直接返回true就行了
  }

  function getOneRow($sql){  //执行一条返回一行数据的语句,返回一维数组
 $result =$this->query($sql); // 一行代替几行代码
  $arr=mysql_fetch_assoc($result);
  mysql_free_result($result);// 提前释放连接资源 释放内存空间(销毁结果集),否则等到页面结束才自动销毁  这是PHP语言的一个优势
  return $arr;
  }

  function getRows($sql){ // 执行一条返回多行数据的语句,返回二维数组
     $result=$this->query($sql);

  $array=array();
  while ($arr=mysql_fetch_assoc($result)) {
      # code...
      $array[]=$arr; // 此时就为二维数组了
  }

  return $array;

  }

  function getOnedata($sql){ //执行一条返回一个数据的语句,返回一个直接值
     $result=$this->query($sql);// 一行代替上面几行
    $arr=mysql_fetch_row($result);
    $data=$arr[0];  // 因为只取出一行,所以直接取出索引为0的数组即可了
    return $data;

  }

  function query($sql){  // 仅仅用于执行sql语句,然后直接返回sql执行的结果,然后给其他函数调用
   $result=mysql_query($sql);
    if($result===false){
        echo "数据执行失败";
        echo "请参考如下信息:".mysql_error();
        die();
    }echo "数据执行成功";
    return $result;
  }

}


 ?>

这个类可以直接封装为'mysql_tool.php'工具类然后引入到别的页面文件中使用,调用代码如下

<?php
require "./mysql_tool.php";
 // $obj=new MYSQL($config);  // sql语句换成 直接调用类

 $obj=MYSQL::GetInstance($config);  
// 因为构造函数被私有化了,所以只能通过类名调用静态函数来实例化一个类


   $sql1="select name from stu ";
   $obj->getRows($sql1);
 var_dump($obj->getOneRow($sql1));
 echo "<br>";
   var_dump($obj->getRows($sql1));
   echo "<br>";
  var_dump($obj->getOnedata($sql1));
  echo "<br>";
    echo $obj->getOnedata($sql1);
?>

猜你喜欢

转载自blog.csdn.net/qq_36622490/article/details/84193710