以下代码在我的本地 WAMP 服务器上运行,但上传时返回此错误:
警告:mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in index.php on line 26
警告:mysql_select_db() [function.mysql-select-db]:无法在第 26 行的 index.php 中建立到服务器的链接错误:无法连接到数据库。请稍后再试。
<?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-]+@([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);
}
?>