我在20分钟前问过这个问题但是当我插入代码时,该网站需要很长时间才能加载。 10分钟后我收到以下错误消息:
Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11
Warning: mysql_connect(): Error while reading greeting packet. PID=2252 in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11
Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11
使用以下代码:
<?php
$conn_error = 'Colud not connect.';
$mysql_host = "localhost:8080";
$mysql_user = "liam";
$mysql_pass = "";
$mysql_db = 'socialnetwork';
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die("Couldn't Connect") ;
mysql_select_db('mysql_db') or die($conn_error);
echo 'Connected!';
?>
答案 0 :(得分:6)
您正在使用字符串作为变量:
mysql_select_db('mysql_db') or die($conn_error);
应该是:
mysql_select_db($mysql_db) or die($conn_error);
应该是×2
// make your db the current db http://php.net/manual/en/function.mysql-select-db.php
$db_selected = mysql_select_db($mysql_db);
if (!$db_selected) {
die ("Can\'t use $mysql_db : " . mysql_error());
}
但真的应该(使用PDO,更改所有代码):
try {
$DB= new PDO('mysql:host=localhost;dbname=socialnetwork', 'liam', '');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
答案 1 :(得分:2)
Windows使用TCP/IP使用localhost以获得更好的性能,因为它具有更少的开销。
$mysql_host = "localhost";
Unix系统使用Unix Domain Socket
$mysql_host = '127.0.0.1';
但是apache使用TCP / IP或Unix Domain Socket连接到PHP。所以无论哪种方式都应该有效。