【发布时间】:2013-02-16 12:07:14
【问题描述】:
我无法通过本地主机上的 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”会连接。
我尝试过创建不同的数据库名称,也尝试过创建新用户并向他们添加完全权限。没有什么对我有用:(
【问题讨论】:
-
不要使用 mysql,使用 mysqli 或 pdo,已弃用
-
它仍然显示数据库选择失败。为什么它不能与 mysql_connect 一起使用?
-
你能用任何其他方法连接到数据库吗,比如 MySQL Workbench 或命令行?
标签: php sql database phpmyadmin xampp