我希望数据库连接与SQL中的类,但我面临错误?
<?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} ;");
?>

hi Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\Anil\object.php on line 12

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\Anil\object.php on line 13 Critical Stop Error: Database Error

donglieshe4692
donglieshe4692 toolong
大约 7 年之前 回复

5个回答



警告拒绝访问用户'root'@'localhost'(使用密码:YES)</ code>表示 与数据库的连接失败,因为您尝试使用错误的用户名或密码进行连接。</ p>

警告 mysqli_error()需要1个参数,0给定</ code>表示 该函数需要1个参数(参见文档),但您没有提供。 它期望 mysqli_connect()</ code>返回的连接标识符作为参数。</ p>
</ div>

展开原文

原文

The warning Access denied for user 'root'@'localhost' (using password: YES) means that the connection to the database failed, because you tried to connect with the wrong username or password.

The warning mysqli_error() expects exactly 1 parameter, 0 given means that the function expected 1 parameter (see the docs), but you supplied none. It expects the connection identifier returned by mysqli_connect() as argument.



更改</ p>

  $ password =''
</ code> </ pre>

</ p>

  $ password =''//如果您没有使用任何密码。
</ code> </ pre> \ n

在</ p>

 公共静态函数中打开(//参数)
</ code> </ pre>
</ div>

展开原文

原文

change

$password = ' '

to

$password = '' // if your are not using any password.

In your

public static function Open( //parameters  )

Check what password is set for your root account.

You're calling the static function

public static function Open($hostname = 'localhost', $username = 'root', $password =' ', $database = 'anil1')

like this:

$DB = DB::Open();

So the default password string is used (which you set to " "). Change it according to your password.

BTW, never ever leave your MySQL root account without a password.



我认为localhost上的root用户有空密码。 但是 Open </ code>方法中的默认值不为空(它是一个空格)。</ p>

  public static function Open($ hostname ='localhost',$  username ='root',$ password ='',$ database ='anil1'){
</ code> </ pre>
</ div>

展开原文

原文

I think that your root user on localhost has empty password. But default value in Open method is not empty (it is a space).

public static function Open($hostname = 'localhost', $username = 'root', $password ='', $database = 'anil1') {



替换公共静态函数打开($ hostname ='localhost',$ username ='root',$ password =' ',$ database ='anil1')</ code>这行代码与下面的行</ p>

 公共静态函数打开($ hostname ='localhost',$ username =  'root',$ password ='',$ database ='anil1')
</ code> </ pre>

我认为它现在可以正常工作。</ p>
</ div >

展开原文

原文

Replace Your public static function Open($hostname = 'localhost', $username = 'root', $password =' ', $database = 'anil1') this line of code with the below line

public static function Open($hostname = 'localhost', $username = 'root', $password ='', $database = 'anil1')

I think it will work now.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐