2016-12-19 77 views
0

我面對這個錯誤與BlueHost的服務器的數據庫連接:BlueHost的遠程訪問數據庫

警告:mysql_connect()函數:無連接可以作出,因爲 目標機器積極地拒絕它。在 C:\ XAMPP \ htdocs中\ besttour \ INC \ db_con.inc2.php第9行

無連接可以作出,因爲目標機器積極地拒絕它。

以下是一個

ini_set('display_errors',1); 

    $Server = "182.185.168.229"; 
    $User = "username"; 
    $Password = "password"; 
    $DB = "databasename"; 
    $DomainName = "http://besttours.com.sg/";  
    $link = mysql_connect($Server, $User, $Password) or die(mysql_error()); 
    if($link) 
     echo "connected to live database"; 
    mysql_select_db($DB, $link) or die(mysql_error()); 

回答

0

嘗試是這樣的代碼:

 // Define connection info 
    $dbhost = ip; 
    $dbuser = username; 
    $dbpass = password; 
    $dbname = databseName; 
    $dbport = databasePort; 

    // create the connection and see if it connects or fails 
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport); 
    if ($conn->connect_errno) 
     { 
      echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error; 
     } 

    // define your query 
    $query = " INSERT QUERY HERE"; 

    // then execute the query and save the result set 
    if ($result = $conn->query($query)) 
     { 
      //leave this here 
     } 

注意:在文件的頂部定義的變量是非常危險的。它允許任何人看到你的連接信息。您想要將連接信息放置在Web服務器/網站根目錄之外的PHP文件中。

另請注意:我看到您使用的mysql_connect已被棄用。任何安裝的PHP新版本都不會接受該命令。 Link to that here.請使用較新的MySQLi。 Which can be found here.

0

它不是一個代碼問題它的藍色主機設置問題,它通常只支持來自其本地主機的連接,但你可以在你的cpanel中覆蓋這個,只需去ypu cpanel,數據庫部分,並選擇遠程mysql。 你可以添加你的公共IP地址。如果您不知道什麼是您的公共IP,請轉至http://ip.w69b.com/