2017-05-29 350 views
-2

我正試圖在phpstorm中連接到mysql數據庫,但我沒有成功。 我會感謝您的幫助。如何使用php連接MySQL數據庫(xampp)?

<?php 
 
$servername = "http://localhost:8012"; 
 
$username = "root"; 
 
$password = ""; 
 

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

 
// Check connection 
 
if ($conn->connect_error) { 
 
    die("Connection failed: " . $conn->connect_error); 
 
}else{ 
 
die("Connected successfully"); 
 
}

的錯誤是:502錯誤的網關

+1

請添加您的代碼。 – shubham715

+0

來自'connect_error'的錯誤是什麼?也可以嘗試將'$ servername'value更改爲'localhost'。您的IDE絕對不是問題 – Akintunde007

+0

502錯誤的網關 – yoni

回答

0

您可以連接數據庫這樣的:

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$database = ""; 
$port = "8012"; 

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

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
}else{ 
die("Connected successfully"); 
} 
+0

502錯誤的網關 – yoni

+0

你能檢查錯誤日誌嗎? –

+0

加載資源失敗:服務器響應狀態爲502(Bad Gateway) – yoni

0

<?php 
 
$servername = "localhost:8012"; 
 
$username = "root"; 
 
$password = ""; 
 
$database = "dbname"; 
 

 
// Create connection 
 
$conn = mysqli_connect($servername, $username, $password,$database) or die(mysqli_error($conn); 
 

 
echo "Connection Successful";

相關問題