<?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()预计在第13行的C:\ xampp \ htdocs \ Anil \ object.php中给出1个参数0 严重停止错误:数据库错误
答案 0 :(得分:0)
更改
$password = ' '
到
$password = '' // if your are not using any password.
在你的
中public static function Open( //parameters )
答案 1 :(得分:0)
检查为root帐户设置的密码。
您正在调用静态函数
public static function Open($hostname = 'localhost', $username = 'root', $password =' ', $database = 'anil1')
像这样:
$DB = DB::Open();
因此使用默认密码字符串(您设置为“”)。根据您的密码更改它。
顺便说一句,在没有密码的情况下永远不要离开你的MySQL root账户。答案 2 :(得分:0)
警告Access denied for user 'root'@'localhost' (using password: YES)
表示与数据库的连接失败,因为您尝试使用错误的用户名或密码进行连接。
警告mysqli_error() expects exactly 1 parameter, 0 given
表示函数预期有1个参数(请参阅the docs),但您没有提供。它期望mysqli_connect()
返回的连接标识符作为参数。
答案 3 :(得分:0)
我认为localhost上的root用户有空密码。但Open
方法中的默认值不为空(它是一个空格)。
public static function Open($hostname = 'localhost', $username = 'root', $password ='', $database = 'anil1') {
答案 4 :(得分:0)
使用以下行替换您的public static function Open($hostname = 'localhost', $username = 'root', $password =' ', $database = 'anil1')
这行代码
public static function Open($hostname = 'localhost', $username = 'root', $password ='', $database = 'anil1')
我认为现在可行。