我的本地WAMP的服務器上的下面的代碼炒鍋,但是當它上傳返回此錯誤:誤差mysql_select_db時上傳至服務器,但不是本地
警告:mysql_select_db()[function.mysql選-DB] :不能(2)中的index.php通過套接字 '/var/run/mysqld/mysqld.sock' 連接到本地MySQL服務器上線26
警告:mysql_select_db()[function.mysql選-db]:到服務器的連接無法在index.php中建立了26行 錯誤:無法連接到數據庫。請稍後再試。
<?php
session_start();
date_default_timezone_set("America/Los_Angeles");
$dbhost = '';
$dbuser = '';
$dbpass = '';
$dbname = '';
if ($_POST)
{
$email = trim($_POST['email']);
$ip_orig = $_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip_orig);
$date = date('Y-m-d');
$time = date('H:i:s');
if(empty($_POST['email'])) {
$_SESSION["errorMsg"] = "Please enter your email.";
header("Location: index.php");
exit;
}
if(!preg_match("/^[_\.0-9a-zA-Z-][email protected]([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", ($_POST['email']))) {
$_SESSION["errorMsg"] = "Incorrect email format; please try again.";
header("Location: index.php");
exit;
}
$db = @mysql_pconnect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
if(!$db){
echo "Error: Could not connect to the database. Please try again later.";
exit;
}
$sql = "SELECT newsletter_email FROM Newsletter WHERE newsletter_email='".$email."'";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
{
$_SESSION["errorMsg"] = "Sorry, that email address is already in our database.";
header("Location: index.php");
exit;
}
$sql = "INSERT INTO Newsletter(newsletter_email, newsletter_ip, newsletter_date, newsletter_time) VALUES('".$email."', '".$ip."', '".$date."', '".$time."')";
$result = mysql_query($sql);
$_SESSION["errorMsg"] = "Thanks! We will be in touch with you soon.";
header("Location: index.php");
exit;
mysql_close($db);
}
?>
可能是因爲你設置的主機,用戶名,密碼和數據庫名稱爲空字符串... – 2013-05-06 22:54:07
確定MySQL服務在服務器上運行? – Barmar 2013-05-06 23:03:11
在發佈之前,我已經拿到了密碼,用戶名和主機。 – acidking 2013-05-07 06:43:01