Tony C
Tony C

Reputation: 568

MySQL Database Connection Problems

I recently set up a a database, and made a code that allowed people to register for a members page on this website I'm building. The code I used has worked before, many times, but for some reason I keep getting this error message when I try to submit data as a test:

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

I've tried everything I know, but nothing works. I think I'm just missing something, here's the php code that sends the data (however, nothing in the code seems to be wrong or out of place):

<?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");
}
   ?>

Any ideas what is causing the MySQL connection error?

Upvotes: 1

Views: 270

Answers (1)

Neil
Neil

Reputation: 55382

System error 111 is ECONNREFUSED, so there isn't anything listening on the specified host; either the host is incorrect or MySQL isn't running on it.

Upvotes: 1

Related Questions