<?php
class Database_Object
{
protected static $DB_Name;
protected static $DB_Open;
protected static $DB_Conn;
protected function __construct($hostname, $username, $password, $database)
{
self::$DB_Name = $database;
self::$DB_Conn = mysqli_connect($hostname, $username, $password, $database);
if (!self::$DB_Conn) { die('Critical Stop Error: Database Error<br />' . mysqli_error()); }
mysqli_select_db(self::$DB_Name, self::$DB_Conn);
}
private function __clone() {}
public function __destruct(){}
}
class DB extends Database_Object
{
public static function Open($hostname = 'localhost', $username = 'root', $password =' ', $database = 'anil1')
{
if (!self::$DB_Open)
{
self::$DB_Open = new self($hostname, $username, $password, $database);
}
else
{
self::$DB_Open = null;
self::$DB_Open = new self($hostname, $username, $password, $database);
}
return self::$DB_Open;
}
public function qry($sql, $return_format = 0)
{
$query = mysqli_query($sql, self::$DB_Conn) OR die(mysqli_error());
switch ($return_format)
{
case 1:
$query = mysqli_fetch_row($query);
return $query;
break;
case 2:
$query = mysqli_fetch_array($query);
return $query;
break;
case 3:
$query = mysqli_fetch_row($query);
$query = $query[0];
return $query;
default:
return $query;
}
}
}
//$dbconnect = new DB_Class();
$DB = DB::Open();
$result = $DB->qry(" {SELECT Firstname FROM persons} ;");
?>
嗨警告:mysqli_connect()[function.mysqli-connect]:(28000/1045):C:\ xampp \ htdocs \ Anil \ object.php中用户'root'@'localhost'(使用密码:YES)的访问被拒绝在第 12 行
警告:mysqli_error() 需要 1 个参数,在第 13 行的 C:\xampp\htdocs\Anil\object.php 中给出 0 严重停止错误:数据库错误