你好亲爱的 可能我为这个讨论选择的主题可能不是很合适,但让我们来解决问题
在 PHP 中,我有一个名为的类DB,在此class我编写数据库关系以连接到数据库:
class DB {
private static $_dbConnect = null,
$dbUser = DB_USER,
$dbPass = DB_PASS,
$dbName = DB_NAME,
$dbTable = null;
function __construct($dbTable) {
self::$dbTable = $dbTable;
if (!isset(self::$_dbConnect) || self::$_dbConnect == null) {
try {
self::$_dbConnect = new PDO("mysql:host=localhost;dbname=".self::$dbName,self::$dbUser,self::$dbPass);
$isResult = self::$_dbConnect->prepare("SHOW TABLES FROM ".self::$dbName." LIKE '${dbTable}'");
if ($isResult->rowCount() == 0) {
$isQuery = "";
if ($dbTable == DB_TABLES['user']) {
$isQuery = ""; //write user table query
} else if ($dbTable == DB_TABLES['admin']) {
$isQuery = ""; //write admin table query
}
self::$_dbConnect->prepare($isQuery);
}
} catch (PDOException $e) {
throw $e->getMessage();
}
}
}
private function ifString($strings=[],$sep='&') {}
private function stringSep($strings=[]) {}
public function existValue($strings=[]) {}
public function insertTB($data=[],$ifArray=[]) {}
public function getDataTB($ifArray=[]) {}
public function updateTB($data=[],$ifArray) {}
public function deleteTB($ifArray) {}
public function getAllRecords($ifArray=[]) {}
function __destruct() {
if (isset(self::$_dbConnect) || self::$_dbConnect != null) {
self::$_dbConnect = null;
}
}
}
但问题是因为我在 中定义了数据库连接__construct,当我同时运行它的两个实例时会遇到麻烦class:
$DB['user'] = new DB(DB_TABLES['user']);
$DB['admin'] = new DB(DB_TABLES['admin']);
问题是现在当我尝试使用时$DB['user'],事情已经完成了$DB['admin']
有解决方案的朋友吗?
翻阅古今