Database Selection Failed. Unknown Database error

2019-06-13 23:58发布

I am having trouble with selecting my database via PHP script on localhost.

I am 100% sure that db name is spelled correctly and infact it appears on phpmyAdmin very well and only when I am trying to connect to it by running a PHP script on localhost it displays the following error:

Database selection failed Unknown database 'fokrul_justdeals'

My PHP code is here:

<?php
    class database{
        public $connection;
        // the user for the database
        public $user = 'root';  
        // the pass for the user
        public $pswd = '';
        // the db from where you want to parse the info  
        public $db = 'fokrul_justdeals';
        // the host where db is located
        public $host = 'localhost';

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

        private function connect(){
            $this->connection = mysql_connect("$host", "$user", "$pswd") or die("Database connection failed ". mysql_error());
            if($this->connection){
                // we select the db that we want to work with
                mysql_select_db($this->db, $this->connection) or die("Database selection failed " . mysql_error());
            }
        }

I have read thousands of forums and I am doing everything as I shall be. But don't know what is going wrong here?

One interesting thing is that out of all databases only system generated db 'mysql' connects if I change the db name to 'mysql' in PHP script.

I have tried creating different db names and also tried creating new users and adding full privileges to them. Nothing has worked for me :(

1条回答
姐就是有狂的资本
2楼-- · 2019-06-14 00:29
$this->connection = mysql_connect("$host", "$user", "$pswd") or die("Database connection failed ". mysql_error());

You should use

$this->host,$this->user,$this->pswd
查看更多
登录 后发表回答