参考:https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
php + mysql操作封装 ,方便 自我封装php框架使用,或者小型框架(yaf等)
目录:
1, mysql.class.php
<?php
/**
* /PHPMySQLi/MysqliDb.php : https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
* @具体使用看文档
*/
define("APP_ROOT", dirname(__FILE__));
require_once APP_ROOT . '/PHPMySQLi/MysqliDb.php';
class mysql
{
protected $db_w = null; // 主库,【主写】
protected $db_r = null; // 从库,【从读】
public function __construct()
{
// parent::__construct();
$db_config = array(
'host' => "127.0.0.1",
'username' => "root",
'password' => "",
'db' => "",
'port' => "3306",
'prefix' => "",
'charset' => "utf8"
);
// $db_config 可以是多维数组,方便主从库切换,参数判断
$db = new MysqliDb($db_config);
// 在mysqli new mysqli实例上创建和存储
$db->addConnection('slave_1', $db_config);
try {
// 设置要在下一个查询中使用的连接名称
$this->db_w = $db->connection('default'); // 主库,【主写】
$this->db_r = $db->connection('slave_1'); // 从库,【从读】
} catch (Exception $e) {
echo $e->getMessage();
die();
}
}
public function __destruct()
{
MysqliDb::getInstance()->disconnectAll();
}
/**
* php的curd,具体看代码 /PHPMySQLi/MysqliDb.php
* @return array
* @throws Exception
*/
public function getList()
{
$sql = "SELECT * FROM `users` WHERE id=1";
$rows = $this->db_r->rawQuery($sql);
return $rows;
}
}