小编典典

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

sql

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

我100%确定数据库名称拼写正确,并且实际上它很好地出现在phpmyAdmin上,并且仅当我尝试通过在本地主机上运行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”会连接。

我尝试创建不同的数据库名称,还尝试创建新用户并向他们添加完全特权。什么都没有为我工作:(


阅读 107

收藏
2021-05-16

共1个答案

小编典典

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

你应该用

$this->host,$this->user,$this->pswd
2021-05-16