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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Pet Net 需求文档
下一篇:UML入门介绍

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月02日 18时52分25秒