Files
juipphp/fastphp/db/Db.php
wanyongkang a331e3f1d5 初始提交
2020-10-03 17:23:32 +08:00

39 lines
953 B
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace fastphp\db;
use PDO;
use PDOException;
/**
* 数据库操作类
* 其属性$pdo为静态属性所以在页面执行周期内
* 只要一次赋值,以后获取的还是首次赋值的内容
* 这里就是pdo对象这样可以保证运行期间只有
* 一个数据库连接对象,简单的单例模式
* Class Db
* @package fastphp\db
*/
class Db
{
private static $pdo = null;
public static function pdo(){
if(self::$pdo !== null){
return self::$pdo;
}
try{
$dns = sprintf('mysql:host=%s;post=%s;dbname=%s;chartset=utf8',DB_HOST,DB_PORT,DB_NAME);
$option = array(PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC);
self::$pdo = new PDO($dns,DB_USER,DB_PASS,$option);
self::$pdo->query("set names utf8");
return self::$pdo;
} catch (PDOException $e){
exit($e->getMessage());
}
}
}