2013-03-01 95 views
0

我在通過本地主機上的PHP腳本選擇我的數據庫時遇到問題。數據庫選擇失敗。未知數據庫錯誤phpmyAdmin

我100%確信數據庫名稱拼寫正確,並且INFACT它出現在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腳本中將db名稱更改爲'mysql'時,系統纔會生成db'mysql'連接。

我試着創建不同的數據庫名稱,並嘗試創建新用戶並向他們添加完整權限。什麼都沒有爲我工作:(

+3

不要使用mysql,使用的mysqli或PDO,它棄用 – Sam 2013-03-01 21:20:55

+0

它仍然顯示數據庫選擇失敗。爲什麼它不與工作mysql_connect? – 2013-03-01 21:31:43

+0

你可以通過任何其他方法(如MySQL Workbench或命令行)連接到數據庫? – Voitcus 2013-03-01 21:33:57

回答

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

您應該使用

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

感謝隊友。現在它說這個 數據庫連接失敗用戶'root'@'localhost'的訪問被拒絕(使用密碼:NO ) – 2013-03-01 21:36:40

+0

您的用戶或通行證或主機不正確 – MIIB 2013-03-01 21:37:50

+0

正如你可以從我上面的代碼(PHP)看到的,我使用root用戶,它沒有任何密碼,所以它保持空白。我檢查了phpmyAdmin root用戶對fokrul_justdeals db的完整權限 – 2013-03-01 21:39:21