2014-01-14 76 views
0

我有一個簡單的PHP代碼來顯示與數據庫的連接是否已建立。它有一個HTML部分,但它似乎沒有建立連接。使用的數據庫是MySql。這些表格不是作爲程序的一部分創建的。無法使用Mysql在PHP中建立與數據庫的連接

需要一些建議....

謝謝....提前。

這裏是代碼...

<h1>LOGIN PAGE </h1> 
<form action="" method="post"> 
Username <input name="usname" type="text" /> 
Password <input name="paswd" type="password" /> 
<input name="submit" type="submit" /> 
</form> 

<?php 
if (isset($_POST['submit'])) { 
    $uname = $_POST['usname']; 
    $pswd = $POST['paswd']; 
    $conn = mysqli_connect("localhost","root","archana","details"); 
    if(mysqli_connect_errorno()) 
    { 
     echo "Failed to connect to Mysql" ; 
    } 
    else 
    { 
     echo "connection established"; 
    } 
    $data = mysqli_query($conn, "SELECT * FROM user"); 
    while($a = mysqli_fetch_array($data)) 
    { 
     echo $a['username']; 
    } 
    mysqli_close($conn); 
} 
?> 
+1

確保你的用戶和密碼是正確的mysqli_connect。同時檢查細節數據庫是否存在。 –

+0

當前代碼回顯是什麼?它會顯示這條消息嗎?'「連接到Mysql失敗」' –

+3

'mysqli_connect_errorno()'應該是'mysqli_connect_errno()' –

回答

0

我重寫基於馬塞爾巴爾澤爾代碼註釋

<h1>LOGIN PAGE </h1> 
<form action="" method="post"> 
Username <input name="usname" type="text" /> 
Password <input name="paswd" type="password" /> 
<input name="submit" type="submit" /> 
</form> 

<?php 
if (isset($_POST['submit'])) { 
    $uname = $_POST['usname']; 
    $pswd = $POST['paswd']; 
    $conn = mysqli_connect("localhost","root","archana","details"); 
    if(mysqli_connect_errno()) 
    { 
     echo "Failed to connect to Mysql. Error: ".mysqli_connect_errno() ; 
    } 
    else 
    { 
     echo "connection established"; 
    } 
    $data = mysqli_query($conn, "SELECT * FROM user"); 
    while($a = mysqli_fetch_array($data)) 
    { 
     echo $a['username']; 
    } 
    mysqli_close($conn); 
} 
?> 
相關問題