Hi guys

Please can you help me, I am having trouble with my login page on my website. When I input the username and password, it said that it can't connect to local MySQL server through socket.

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/mark107.awardspace.com/login/login-exec.php on line 15
Failed to connect to server: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

why did i get this error??

Please help!!!!!!!!!!!

Thanks,
Mark

    Was this code that was working previously? If so, your mysql server probably just needs to be started. If it's new, you probably just don't have all the details right in your mysql_connect function call.

      Thanks for the reply, the code was working previously but it just new for me that i found on the website sample. I am using on free web hosting to test my login before i would then buying my own website. Please can you confirm in which directory that mysql should be placed and should be called??

      Something like this:
      mark107.awardspace.com/login/login-exec.php

      But I wasn't too sure, if you need to see my php code, here it is:

      <?php
      	//Start session
      	session_start();
      
      //Include database connection details
      require_once('config.php');
      
      //Array to store validation errors
      $errmsg_arr = array();
      
      //Validation error flag
      $errflag = false;
      
      //Connect to mysql server
          $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      if(!$link) {
      	die('Failed to connect to server: ' . mysql_error());
      }
      
      //Select database
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) {
      	die("Unable to select database");
      }
      
      //Function to sanitize values received from the form. Prevents SQL injection
      function clean($str) {
      	$str = @trim($str);
      	if(get_magic_quotes_gpc()) {
      		$str = stripslashes($str);
      	}
      	return mysql_real_escape_string($str);
      }
      
      //Sanitize the POST values
      $login = clean($_POST['login']);
      $password = clean($_POST['password']);
      
      //Input Validations
      if($login == '') {
      	$errmsg_arr[] = 'Login ID missing';
      	$errflag = true;
      }
      if($password == '') {
      	$errmsg_arr[] = 'Password missing';
      	$errflag = true;
      }
      
      //If there are input validations, redirect back to the login form
      if($errflag) {
      	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      	session_write_close();
      	header("location: login-form.php");
      	exit();
      }
      
      //Create query
      $qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
      $result=mysql_query($qry);
      
      //Check whether the query was successful or not
      if($result) {
      	if(mysql_num_rows($result) == 1) {
      		//Login Successful
      		session_regenerate_id();
      		$member = mysql_fetch_assoc($result);
      		$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
      		$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
      		$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
      		session_write_close();
      		header("location: member-index.php");
      		exit();
      	}else {
      		//Login failed
      		header("location: login-failed.php");
      		exit();
      	}
      }else {
      	die("Query failed");
      }
      ?>

      Hope you can help to get my situation to resolve.

      Thanks,
      Mark

        So you went from a paid hosting account to a new free hosting account on another server? Does the free account even offer a MySQL database? If so, did you transfer your MySQL database to the new server?

          No, i am on free hosting at the moment. I want to get everything working with my mysql. Please look my post carefully...

            Here I have set up the database username and the password so i have input them on login-exec.php included the db host name, here it is the sample.

            $link = mysql_connect(db123.testsite.com, myname_data, mypassword);

            Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'db123testsitecom' (1) in /home/www/myname.testsite.com/login/login-exec.php on line 15
            Failed to connect to server: Unknown MySQL server host 'db123testsitecom' (1)

            I am getting the error. What's wrong?? Have i missing something?? 🙁

            Thanks,
            Mark

              I'm pretty sure the first error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" means that your MySQL server (mysqld) is not running. Contact your host if you see that message.

              The second error, I'm not sure if that's the actual code but you would need to enclose each field. For example:

              $link = mysql_connect('db123.testsite.com','myname_data','mypassword');
              

                Thanks for the help, I have convert the code and it seen to work but I got the error message saying 'Query failed'. What do that mean and how can I resolve it??

                Thanks,
                Mark

                  It's easier to troubleshoot code when you use mysql_error(). Example:

                  $link = mysql_connect('db123.testsite.com','myname_data','mypassword')
                    or die("Connection to db123.testsite.com failed - ".mysql_error());
                  
                  $db=mysql_select_db('dbname',$link)
                    or die("Cannot select database dbname - ".mysql_error());
                  
                  $select="select some_field from some_table where some_condition";
                  $result=mysql_query($select)
                    or die("Select statement \"$select\" failed - ".mysql_error());
                  

                  This will give you more specific reasons why something is failing. It makes it much easier to troubleshoot. Also, you will note I am putting in specific information in the die() statement, so I know where in the code something is failing. Again, it makes for much easier troubleshooting, especially when your code runs into the thousands of lines...

                    Thanks for the help. I have found the problem, resolved!!!!!!!!!!

                      Write a Reply...