PHP 连接MySQL 的单态类
发布日期:2021-10-01 03:31:34
浏览次数:4
分类:技术文章
本文共 1155 字,大约阅读时间需要 3 分钟。
<?php /** * author: xueming */ class MySQLConnFactory { private $conn; static private $_mysqlconn; //Construct-->connect DB private function __construct($host,$username,$password) { $this->conn = mysql_connect($host,$username,$password); $this->query("SET NAMES 'utf8'"); return $this->conn; } private function __clone(){} public static function getSingleMySQLConn($host,$username,$password) { if(FALSE==(self::$_mysqlconn instanceof self)) { self::$_mysqlconn = new self($host,$username,$password); } return self::$_mysqlconn; } //select DB public function select_db($database) { $this->result = mysql_select_db($database); return $this->result; } //Query public function query($query) { return $this->result = mysql_query($query, $this->conn); } //fetch result public function fetch_array($fetch_array) { return $this->result = mysql_fetch_array($fetch_array, MYSQL_ASSOC); } //close public function close() { return $this->result = mysql_close($this->conn); } } ?>调用方式如下
$sql = "select * from test"; $connector = MySQLConnFactory::getSingleMySQLConn($db_server,$db_user,$db_pwd); $connector -> select_db($db_name); $result = $connector -> query($sql);
转载地址:https://blog.csdn.net/iteye_1789/article/details/82209803 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月02日 18时52分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode刷题篇】leetcode121买卖股票的最佳时机
2019-04-26
【面试篇】Java多线程并发-Java关键字volatile详解
2019-04-26
【面试篇】Java的代理模式-静态代理和动态代理详解
2019-04-26
【面试篇】 Java对象拷贝(对象克隆 对象复制)
2019-04-26
【Leetcode刷题篇】leetcode64 最小路径和
2019-04-26
【Leetcode刷题篇】leetcode79 单词搜索
2019-04-26
【Leetcode刷题篇】leetcode300 最长上升子序列
2019-04-26
【Leetcode刷题篇】leetcode394 字符串解码
2019-04-26
【Leetcode刷题篇】leetcode152 乘积最大数组
2019-04-26
【Leetcode刷题篇】leetcode56 合并区间
2019-04-26
【Leetcode刷题篇】leetcode210 课程表II
2019-04-26
【Leetcode刷题篇】leetcode207 课程表
2019-04-26
【Leetcode刷题篇】leetcode322 零钱兑换
2019-04-26
【Leetcode刷题篇】leetcode437 路径总和III
2019-04-26
【Linux篇】Linux常用命令之性能优化
2019-04-26
【面试篇】JVM体系
2019-04-26
【Leetcode刷题篇】leetcode406 根据身高重建队列
2019-04-26
【Leetcode刷题篇】leetcode581 最短无序连续子数组
2019-04-26
【Leetcode刷题篇】leetcode538 把二叉搜索树转换为累加树
2019-04-26
【多线程与高并发】线程的优先级是怎么回事?
2019-04-26