2013-03-16 52 views
1

當是我使用錯誤信息連接到我無法連接到數據庫</p> <p>這裏數據庫

$con = mysql_connect('host', 'user', 'pass'); 
mysql_select_db('database_name', $con); 

這裏的代碼,結果我得到

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3

+3

您的代碼已棄用。 – shnisaka 2013-03-16 19:15:35

+0

不使用mysql_connect 使用http://www.php.net/manual/en/mysqli.construct.php 或http://www.php.net/manual/en/pdo.construct.php – 2013-03-16 19:17:38

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – 2013-03-16 19:20:36

回答

1

最好使用PDO或Mysqli。我更喜歡PDO,因爲它也支持除mysql之外的其他數據庫,因此如果需要,可以更輕鬆地進行遷移。

你可以很容易地通過

$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>', '<USERNAME>', 'PASSWORD'); 

欲瞭解更多信息的連接:http://php.net/manual/en/book.pdo.php

如果你想使用的mysqli使用:

$mysqli = new mysqli("localhost", "user", "password", "database"); 
0

嘗試使用此代碼

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); 

if (!$link) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
      . mysqli_connect_error()); 
} 

echo 'Success... ' . mysqli_get_host_info($link) . "\n"; 

mysqli_close($link); 

來源:http://www.php.net/manual/en/mysqli.construct.php

0
<?php 
$mysqli = new mysqli("localhost", "user", "password", "database"); 
if ($mysqli->connect_errno) { 
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .  $mysqli->connect_error; 
} 
echo $mysqli->host_info . "\n"; 

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306); 
if ($mysqli->connect_errno) { 
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .  $mysqli->connect_error; 
} 

echo $mysqli->host_info . "\n"; 
?> 
3

嘗試使用此代碼

$ CON =的mysql_connect( 「宿主」, 「用戶」, 「通」);
mysql_selectdb(「database_name」,$ con);

+1

與提供的樣本來源有何不同。 – Tchoupi 2013-03-16 19:44:33

+2

@Mathieu它們不是mysql_selectdb和mysql_select_db之間的區別嗎? – 2013-03-16 19:56:45

+0

那麼如何回答這個問題呢? – Tchoupi 2013-03-16 20:11:56

0
$con = mysql_connect('host', 'user', 'password'); 
if (!$con) { 
    die('Not connected : ' . mysql_error()); 
} 

$db = mysql_select_db('database_name', $con); 
if (!$db) { 
    die ('Can\'t use database_name : ' . mysql_error()); 
}