直接上代码MySqlDBSingle.class.php
<?php
/**
* Created by PhpStorm.
* User: Mengk
* Date: 2018/8/29
* Time: 10:38
*/
header("content-type:text/html;charset=utf-8");
class MySqlDBSingle
{
private $host; //主机IP
private $port; //端口号
private $user; //用户名
private $pwd; //密码
private $charset; //字符编码
private $dbname; //连接的数据库
private static $instance; //保存MySQL的唯一实例
private $link; //保存数据库连接对象 可以省略
/**
* 初始化连接数据库参数
* @param $config
*/
private function initParams($config)
{
$this->host = isset($config['host']) ? $config['host'] : '';
$this->port = isset($config['port']) ? $config['port'] : '';
$this->user = isset($config['user']) ? $config['user'] : '';
$this->pwd = isset($config['pwd']) ? $config['pwd'] : '';
$this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
$this->dbname = isset($config['dbname']) ? $config['dbname'] : '';
}
/**
* 连接数据库
*/
private function connect()
{
$this->link = @mysql_connect("{$this->host}:{$this->port}", $this->user, $this->pwd) or die('数据库连接失败');
}
/**
* 此方法用来执行sql语句
* 如果是数据查询语句 成功返回结果集 失败返回false
* 如果是数据操作语句 成功返回true 失败返回false
* @param $sql
* @return resource
*/
public function query($sql)
{
if (!$result = mysql_query($sql, $this->link)) {
echo 'sql语句执行失败<br>';
echo '错误编号:' . mysql_errno(), '<br>';
echo '错误信息:' . mysql_error(), '<br>';
echo '错误的sql语句:' . $sql, '<br>';
exit;
}
return $result;
}
/**
* 设置字符集编码
*/
private function setCharset()
{
$sql = "set names {$this->charset}";
$this->query($sql);
}
/**
* 选择数据库
*/
private function selectDB()
{
$sql = "use {$this->dbname}";
$this->query($sql);
}
/**
* 私有的构造方法防止类的外部实例化对象
* MySqlDBSingle constructor.
* @param $config 连接数据库的参数
*/
private function __construct($config)
{
$this->initParams($config);
$this->connect();
$this->setCharset();
$this->selectDB();
}
/**
* 私有的克隆方法 用来阻止类的外部克隆对象
*/
private function __clone()
{
// TODO: Implement __clone() method.
}
/**
* 公有的静态方法获取唯一的实例
* @param array $config
* @return MySqlDBSingle
*/
public static function getInstance($config = array())
{
if (!self::$instance instanceof MySqlDBSingle) {
self::$instance = new MySqlDBSingle($config);
}
return self::$instance;
}
/**
* 从数据库获取所有数据
* @param $sql
* @param string $fetch_type assoc|row|array
* @return array 将表中的数据匹配成二维数组返回
*/
public function fetchAll($sql, $fetch_type = 'assoc')
{
$rs = $this->query($sql);
$fetch_types = array('assoc','row','array');
if (!in_array($fetch_type,$fetch_types)) {
$fetch_type = 'assoc';
}
$fetch_fun = 'mysql_f etch_'.$fetch_type;
$array = array();
while ($rows = $fetch_fun($rs)) {
$array[] = $rows;
}
return $array;
}
/**
* 查询列表的第一条记录
* @param $sql
* @param string $fetch_type
* @return mixed|null
*/
public function fetchRow($sql, $fetch_type = 'assoc') {
$rs = $this->fetchAll($sql, $fetch_type);
if(!empty($rs)) {
return $rs[0];
}
return null;
}
/**
* 查询列表的第一行第一列
* @param $sql
* @return null
*/
public function fetchColumn($sql) {
$rs = $this->fetchRow($sql,'row');
if(!empty($rs)) {
return $rs[0];
}
return null;
}
}
$config = array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'pwd' => 'root',
'charset' => 'utf8',
'dbname' => 'mydatabase'
);
$db = MySqlDBSingle::getInstance($config);
$rs = $db->fetchAll("select * from my_student",'assoc');
echo '<pre>';
var_dump($rs);echo '<hr>';
$rs = $db->fetchRow("select * from my_student");
echo '<pre>';
var_dump($rs);echo '<hr>';
$rs = $db->fetchColumn("select * from my_student");
echo '<pre>';
var_dump($rs);echo '<hr>';