PHP MySQL:无法连接到数据库

时间:2014-12-11 22:48:54

标签: php mysql sql

我正在尝试连接到php中的数据库,数据库名称为photo_gallery以下是我的代码

database.php中

    define("DB_SERVER", "localhost"); 
    define("SERVER_NAME", "root"); 
    define("DB_PASS" ,"");
    define("DB_NAME", "photo_gallery");

    class MySQLDatabase{

        function __construct(){
            $this->open_connection();
        }

        private $connection ; 
        public function open_connection (){
        $this->connection = mysqli_connect(DB_SERVER, DB_NAME, DB_PASS);
            if(!$this->connection){
                die("Connection failed".mysqli_error());
        }
        else {
            $db_select=  mysqli_select_db($this->connection,DB_NAME);
            if(!$db_select){
                echo 'Database selection failed '.mysqli_error($this->connection);
            }
        }
    }
        public  function query($sql){
            $query = mysqli_query($this->connection,$sql); 
            $this->confirm_query($query);
            return $query;
        }       

        private function confirm_query($result_set){
            $query= mysqli_query($this->connection,$result_set); 
            if(!$query){
                    die('Database query failed '.mysqli_error());
            } 
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysqli_close($this->connection);
                unset($this->connection);
            }
        }       
    }

现在当我跑步时我得到以下错误

  

数据库选择失败用户拒绝访问:'@'@ localhost'。   基地'photo_gallery

我已确认用户名/密码和数据库详细信息,但仍无法解决。请指导我一下

4 个答案:

答案 0 :(得分:0)

您尚未提供用户名 - 您的mysqli_connect(DB_SERVER, DB_NAME, DB_PASS)行未定义用户,因此您尝试使用用户连接到您的数据库。

请尝试mysqli_connect(DB_SERVER, SERVER_NAME, DB_NAME, DB_PASS)

答案 1 :(得分:0)

mysqli-connect的connect方法的第二个参数是USERNAME。 像这样使用它

$ this-> connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASS,DB_NAME);

http://php.net/manual/en/mysqli.construct.php

答案 2 :(得分:0)

您的问题是您为常量选择了错误的名称..

SERVER_NAME 不是数据库用户名的好名称。

作为这个的后果,你用错了..我建议将常量更改为 DB_USER 并在mysqli_connect()函数中正确使用它。

请记住使用真正代表他们所做/参考的名称。

答案 3 :(得分:0)

这是语法:

   $conn= mysqli_connect("myhost","myuser","mypassw","mydb");

以下是代码:

<强> database.php中

    define("DB_SERVER", "localhost"); 
    define("SERVER_NAME", "root"); 
    define("DB_PASS" ,"");
    define("DB_NAME", "photo_gallery");

    class MySQLDatabase{

        function __construct(){
            $this->open_connection();
        }

        private $connection ; 
        public function open_connection (){
        $this->connection = mysqli_connect(DB_SERVER, SERVER_NAME, DB_PASS, DB_NAME);
            if(!$this->connection){
                die("Connection failed".mysqli_error());
        }
        else {
            $db_select=  mysqli_select_db($this->connection,DB_NAME);
            if(!$db_select){
                echo 'Database selection failed '.mysqli_error($this->connection);
            }
        }
    }
        public  function query($sql){
            $query = mysqli_query($this->connection,$sql); 
            $this->confirm_query($query);
            return $query;
        }       

        private function confirm_query($result_set){
            $query= mysqli_query($this->connection,$result_set); 
            if(!$query){
                    die('Database query failed '.mysqli_error());
            } 
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysqli_close($this->connection);
                unset($this->connection);
            }
        }       
    }