数据库选择失败。 phpmyAdmin上的未知数据库错误

时间:2013-03-01 21:19:36

标签: php sql database phpmyadmin xampp

我在localhost上通过PHP脚本选择我的数据库时遇到了麻烦。

我100%确定db名称拼写正确,并且它在phpmyAdmin上显示得非常好,只有当我尝试通过在localhost上运行PHP脚本来连接它时,它才会显示以下错误:

Database selection failed Unknown database 'fokrul_justdeals'

我的PHP代码在这里:

<?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());
            }
        }

我已经阅读了数以千计的论坛,我正在按照自己的意愿行事。但不知道这里出了什么问题?

有一件有趣的事情是,如果我在PHP脚本中将数据库名称更改为“mysql”,那么只有系统生成的数据库'mysql'连接在所有数据库中。

我尝试创建不同的数据库名称,并尝试创建新用户并为其添加完整权限。没有什么对我有用:(

1 个答案:

答案 0 :(得分:2)

$this->connection = mysql_connect("$host", "$user", "$pswd") or die("Database connection failed ". mysql_error());

你应该使用

$this->host,$this->user,$this->pswd