【问题标题】:Error with mysql_select_db when uploaded to server but not locallymysql_select_db 上传到服务器但不是本地时出错
【发布时间】:2013-05-06 22:52:08
【问题描述】:

以下代码在我的本地 WAMP 服务器上运行,但上传时返回此错误:

警告:mysql_select_db() [function.mysql-select-db]: 无法通过 index.php 中的套接字 '/var/run/mysqld/mysqld.sock' (2) 连接到本地 MySQL 服务器第 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);
}
?>

【问题讨论】:

  • 可能是因为您将主机、用户名、密码和数据库名称设置为空字符串...
  • 您确定服务器上正在运行 MySQL 服务吗?
  • 发帖前我把密码、用户名和主机拿走了。
  • 是的 sql 正在运行,我有其他脚本连接到同一个 sql 服务器并使用相同的密码、用户和数据库设置。只有这个脚本有错误,虽然它在我的 wamp 服务器上离线工作。

标签: php sql


【解决方案1】:

您应该在脚本顶部指定数据库用户名、密码、数据库名称和主机。

更新:

<?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-]+@([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>

【讨论】:

  • 主机名"" 被视为"localhost",错误的用户名/密码得到Access denied 错误。所以他的设置不应该导致Could not connect to the database错误。
  • 如果服务器不使用本地 MySQL,或者使用非标准套接字路径,肯定会导致该错误。
  • 你错了。请仔细检查,因为这正是他将遇到的错误。
  • 发帖前我把密码、用户名和主机拿走了。
  • 用脚本更新了我上面的答案。一探究竟。此外,如果在这种情况下它不起作用,则说明您的 MySQL 有问题,所以请确保它正常运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多