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

博主在尝试通过PHP脚本连接本地主机上的数据库时遇到问题,错误提示为未知数据库'fokrul_justdeals'。尽管数据库名在phpMyAdmin中显示正确,但PHP脚本无法成功连接。代码中已经设置了正确的数据库、用户名和密码,且尝试了不同的数据库名和用户权限,但问题仍然存在。有趣的是,只有系统默认的'mysql'数据库可以连接。
摘要由CSDN通过智能技术生成

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:

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 :(

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

You should use

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值