MySQL数据库连接问题

时间:2011-07-20 22:54:41

标签: php mysql

我最近建立了一个数据库,并制作了一个代码,允许人们在我正在建立的这个网站上注册成员页面。我以前使用过的代码很多次,但是由于某种原因,当我尝试将数据作为测试提交时,我不断收到此错误消息:

Failed to connect to server: Lost connection to MySQL server at 'reading initial communication packet', system error: 111

我已经尝试过我所知道的一切,但没有任何效果。我想我只是遗漏了一些东西,这里是发送数据的PHP代码(但是,代码中没有任何内容似乎是错误的或不合适的):

<?php

session_start();


require_once('config.php');


$errmsg_arr = array();


$errflag = false;


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}


$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}


function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}


$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$login = clean($_POST['login']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);


if($fname == '') {
    $errmsg_arr[] = 'First name missing';
    $errflag = true;
}
if($lname == '') {
    $errmsg_arr[] = 'Email';
    $errflag = true;
}
if($login == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($password == '') {
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}
if($cpassword == '') {
    $errmsg_arr[] = 'Confirm password missing';
    $errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
    $errmsg_arr[] = 'Passwords do not match';
    $errflag = true;
}


if($login != '') {
    $qry = "SELECT * FROM members WHERE login='$login'";
    $result = mysql_query($qry);
    if($result) {
        if(mysql_num_rows($result) > 0) {
            $errmsg_arr[] = 'Login ID already in use';
            $errflag = true;
        }
        @mysql_free_result($result);
    }
    else {
        die("Query failed");
    }
}


if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: register-form.php");
    exit();
}



{
    mkdir("somedirectory//$login") or die ("Could not make directory");
}

{
    $ourFileName = "somedirectory/$login/verify.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    fclose($ourFileHandle);

    $ourFileName = "somedirectory/$login/name.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    fclose($ourFileHandle);

}

{

    $myFile = "somedirectory/$login.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "$login\n";
    fwrite($fh, $stringData);
    fclose($fh);
}
{

    $file = '000001.php';
    $newfile = "somedirectory/$login/$login.php";

    if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n";}

}




$qry = "INSERT INTO members(firstname, lastname, login, passwd) VALUES('$fname','$lname','$login','".md5($_POST['password'])."')";
$result = @mysql_query($qry);


if($result) {
    header("location: register-success.php");
    exit();
}else {
    die("Query failed");
}
   ?>

任何想法导致MySQL连接错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

系统错误111是ECONNREFUSED,因此在指定的主机上没有任何监听;主机不正确或MySQL没有运行。