2013-05-06 67 views
0

我的本地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); 
} 
?> 
+0

可能是因爲你設置的主機,用戶名,密碼和數據庫名稱爲空字符串... – 2013-05-06 22:54:07

+0

確定MySQL服務在服務器上運行? – Barmar 2013-05-06 23:03:11

+0

在發佈之前,我已經拿到了密碼,用戶名和主機。 – acidking 2013-05-07 06:43:01

回答

0

您應該在腳本的頂部指定數據庫的用戶名,密碼,數據庫名和主機。

更新:

<?php 
//Start session 
session_start(); 
date_default_timezone_set("America/Los_Angeles"); 

$dbhost = 'localhost'; 
$dbuser = 'dbuser'; 
$dbpass = 'pass'; 
$dbname = 'test'; 

if ($_POST) 
{ 

    //Retrieve data from form and create variables 
    $email = trim($_POST['email']); 
    $ip_orig = $_SERVER['REMOTE_ADDR']; 
    $ip = ip2long($ip_orig); 
    $date = date('Y-m-d'); 
    $time = date('H:i:s'); 

    //Validate data 
    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; 
    } 

    //Log into the server 
    $db = @mysql_pconnect($dbhost, $dbuser, $dbpass); 

    //Select the database desired 
    mysql_select_db($dbname); 

    //If no connection can be made, echo it out to the screen 
    if(!$db){ 
     echo "Error: Could not connect to the database. Please try again later."; 
    exit; 
    } 

    //Check to see that no duplicates exist in the database 
    $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; 
    } 

    //Place into database 
    $sql = "INSERT INTO Newsletter(newsletter_email, newsletter_ip, newsletter_date, newsletter_time) VALUES('".$email."', '".$ip."', '".$date."', '".$time."')"; 

    //Run query and get result back; shouldn't return anything 
    $result = mysql_query($sql); 

    //Redirect user to successful registration page 
    $_SESSION["errorMsg"] = "Thanks! We will be in touch with you soon."; 
    header("Location: index.php"); 
    exit; 

    //Close the database connection 
    mysql_close($db); 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form method="post" action="index.php"> 
<input name="email" value="" /> 
<input type="submit" name="submit" value="Submit" /> 
</form> 

</body> 
</html> 
+0

主機名'「」'被視爲'「localhost」的',和錯誤的用戶名/密碼,獲得'訪問denied'錯誤。所以他的設置不應該導致「無法連接到數據庫」錯誤。 – Barmar 2013-05-06 23:02:45

+0

如果服務器不使用本地MySQL,或者使用非標準套接字路徑,則肯定會導致該錯誤。 – duskwuff 2013-05-06 23:05:22

+0

你錯了。請仔細檢查,因爲那正是他遇到的錯誤。 – NMALKO 2013-05-06 23:06:42

相關問題