2016-05-17 154 views
0

我在嘗試連接到我的數據庫時遇到了一些問題。 我用下面的代碼:爲什麼我無法連接到我的數據庫?

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 

    <title>Playlists</title> 
    <meta name="description" content="test"> 
    <meta name="author" content="SitePoint"> 

    <link rel="stylesheet" href="stylesheets.css"> 

    </head> 

<body> 
     <nav> 
    <a href="index.html"><img src="http://www.samsung.com/us/showcase/milk/img/meta-fb-milk-music.png" alt="Home"></a> 
     <ul> 
      <li><a href="index.html">Home</a></li> 
      <li><a href="">Albums</a></li> 
      <li><a href="playlist.html">Playlists</a></li> 
      <li><a href="">About</a></li> 
     </ul> 
    </nav> 

<div class="content"> 
    <?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 
$database = "dbname"; 

// Create connection 
$conn = new mysqli($servername, $username, $password); 

// Check connection 
if ($conn->connect_error) { 
    die("not fire" . $conn->connect_error); 
} 
echo "fire"; 

@mysql_select_db($database) or die("Unable to select database"); 

mysql_close(); 

?> 
    </div> 

</body> 
</html> 

當然,這些都不是我真正的登錄名,但這些肯定是正確的。 當我打開頁面它說:「fireUnable選擇數據庫」所以與服務器的連接會好起來的,但連接到數據庫不是。有人可以幫我嗎?

+3

因爲你是在'mysql_select_db'這裏 – Saty

+0

我應該改變它的mysqli或MySQL混合'的mysqli與mysql'?我試過這個://創建連接 $ conn = new mysqli($ servername,$ username,$ password); ($ conn-> connect_error){ die(「not fire」。$ conn-> connect_error); } 回聲「火」; @mysqli_select_db($ database)或者die(「無法選擇數據庫」); mysqli_close(); 但後來我得到了同樣的錯誤。 – Bram

+0

閱讀http://php.net/manual/en/mysqli.quickstart.connections.php – Saty

回答

0

您錯過了連接中的數據庫名稱。記住,你正在使用面向對象的風格?試試這個:

<!doctype html> 

    <html lang="en"> 
    <head> 
     <meta charset="utf-8"> 

     <title>Playlists</title> 
     <meta name="description" content="test"> 
     <meta name="author" content="SitePoint"> 

     <link rel="stylesheet" href="stylesheets.css"> 

    </head> 

    <body> 
    <nav> 
     <a href="index.html"><img src="http://www.samsung.com/us/showcase/milk/img/meta-fb-milk-music.png" alt="Home"></a> 
     <ul> 
      <li><a href="index.html">Home</a></li> 
      <li><a href="">Albums</a></li> 
      <li><a href="playlist.html">Playlists</a></li> 
      <li><a href="">About</a></li> 
     </ul> 
    </nav> 

    <div class="content"> 
     <?php 
      $servername = "localhost"; 
      $username = "username"; 
      $password = "password"; 
      $database = "dbname"; 

      // Create connection 

      // Create connection 
      $conn = new mysqli($servername, $username, $password, $database); 


      // Check connection 
      if ($conn->connect_error) { 
       die("not fire: " . $conn->connect_error); 
      } 
      echo "fire"; 

     ?> 


    </div> 

    </body> 
    </html> 
相關問題