2015-01-14 93 views
-1

我是初學者。 我的服務器網絡http://www.barman-team.ir/tt.php 上覆制一個簡單的PHP頁面,我導入SQL數據庫,使用戶&和傳遞(在的cPanel)本地主機(XAMP)上警告:mysql_connect():拒絕用戶訪問

此頁的數據的基礎上,但不要在服務器上運行

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body dir="rtl"> 

<?php 
     $link=mysql_connect("localhost","uname","pass"); 
     mysql_select_db("barmante_ebram",$link); 
     $q="SELECT * FROM site_sug";// 
     $r=mysql_query($q,$link); 
     $line= mysql_fetch_array($r); 
     //while($line== $result->fetch_assoc()){ 
     echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";  
     //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>"; 
     //} 
    ?> 
</body> 
</html> 

錯誤日誌:

Warning: mysql_connect(): Access denied for user 'barmante_ebram_u'@'localhost' (using password: YES) in /home/barmante/public_html/tt.php on line 11 

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 12 

Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 14 

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/barmante/public_html/tt.php on line 15 
+0

有什麼不清楚的錯誤信息?您的實時環境需要與測試環境不同的用戶名和/或密碼。 – Quentin

+1

**警告**:您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068),並應使用[現代替換](http:// php。淨/手動/ EN/mysqlinfo.api.choosing.php)。 – Quentin

回答

0

第一個錯誤是在說,你的密碼,用戶名您所提供的這意味着它無法連接到MySQL和其他錯誤是說你應該爲第二個參數提供資源。你只是交換了參數的順序。使用下面的代碼

<?php 
     $link=mysql_connect("localhost","uname","pass"); 
     mysql_select_db($link,"barmante_ebram"); 
     $q="SELECT * FROM site_sug";// 
     $r=mysql_query($link); 
     $line= mysql_fetch_array($r); 
     //while($line== $result->fetch_assoc()){ 
     echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";  
     //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>"; 
     //} 
    ?> 

注意:*不要使用mysql_ *函數,否則不推薦使用。使用mysqli或PDO準備好的語句 希望這可以幫助你

+0

仍然使用不推薦的'mysql_ *'函數? – Raptor

0

使用這種調試來追蹤錯誤。 我想你的所有警告都與USERNAME或PASSWARD有關。使用正確的用戶名和密碼。它會解決問題。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); 
if (!$link) { 
    die('Not connected : ' . mysql_error()); 
} 

// make foo the current db 
$db_selected = mysql_select_db('foo', $link); 
if (!$db_selected) { 
    die ('Can\'t use foo : ' . mysql_error()); 
} 

使用mysql_error(),每個查詢,這樣你會了解是什麼原因造成的問題(初學者有用)

0

假設你有用戶正確的權限,下面的代碼應該工作。它會告訴你在連接到sql server時是否有錯誤,或者查詢中有任何錯誤。

<?php 
    $db_hostname = 'localhost'; 
    $db_database = 'barmante_ebram'; 
    $db_username = 'uname'; 
    $db_password = 'pass'; 
    $db_link= new MySQLi($db_hostname, $db_username, $db_password, $db_database); 
    if(! $db_link)  //if($db_link->connect_errno > 0) 
     die("Could not connect to database server\n" + $db_link->connect_error); 

    $query = "SELECT * FROM site_sug"; 
    $result = $db_link->query($query); 
    if(!result) 
     die("Error in query".$db_link->error); 
    $row = $result->fetch_assoc(); 
    echo $row['site_suggestion_id'].$line['site_suggestion']; 

>

注意:請經常做錯誤檢查(如果(成功的)死亡(」「)!)。你的代碼沒有任何。這是一個很好的做法。

相關問題