2012-08-29 287 views
0

我想連接到MySQL數據庫的第一次。你能看到下面哪些工作不正常嗎? 我收到第3行的錯誤。 我的服務器地址從Godaddy到我的數據庫會是什麼樣子?我在控制面板中找到了該地址。無法連接到MySQL數據庫

感謝您的任何幫助。之前從未使用PHP進行編程。

<body> 
<?php 
$con = mysql_connect("MydbName.db.3924516.hostedresource.com ","Userid","password"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("my_db", $con); 

$result = mysql_query("SELECT * FROM Gallerys"); 

echo "<table border='1'> 
<tr> 
<th>Thumb Url </th> 
<th>Gallery Url</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
{ 
    echo "<tr>"; 
    echo "<td>" . $row['THUMBURL'] . "</td>"; 
    echo "<td>" . $row['GALLERYURL'] . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

mysql_close($con); 
?> 

</body> 
+8

由於您剛剛瞭解到'mysql_ *',因此我不想破壞您的泡泡,但該擴展程序處於預先棄用階段,至少您需要找到一個使用' mysqli'擴展名或'PDO'。並確保它提到「準備好的陳述」。因爲如果教程沒有,它就是垃圾。 – rdlowrey

+0

你得到的錯誤信息是什麼? –

+0

謝謝我找那個教程。 – CsharpBeginner

回答

3

嘗試檢查驗證設置,您可能使用錯誤的主機,用戶名或密碼。

<?php 
    $con = mysql_connect("usually it is localhost","your MYSQL username","your MYSQL password"); 

// Checking the login details. 
// Example of default xampp login details: $con = mysql_connect("localhost","root",""); 
// Xampp MYSQL default does not have password. 

    if (!$con) // If the login details are wrong, it will should an error. 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 
    ?> 
1

沒有確切的錯誤是很難確定,但在第3行,你有後hostedresource.com一個額外的空間。嘗試刪除主機名末尾和quatation標記之間的空格。像這樣:

$con = mysql_connect("MydbName.db.3924516.hostedresource.com","Userid","password");