0

I am using following code to connect to an sql database on a web hosting server. I am facing the error

Warning: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /srv/disk7/2067595/www/envoycc.atwebpages.com/DB_Connect.php on line 10

The 10th line is $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

My overall code is

config.php

 <?php

 /**
 * Database config variables
 */
define("DB_HOST", "localhost");
define("DB_USER", "2067595_android");
define("DB_PASSWORD", "password");
define("DB_DATABASE", "2067595_android");
?>

DB_Connect.php

<?php
class DB_Connect {
private $conn;

// Connecting to database
public function connect() {
    require_once 'config.php';

    // Connecting to mysql database
    $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

    // return database handler
    return $this->conn;
}
}

?>

DB_Functions.php

<?php

class DB_Functions {

    private $conn;

    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
        $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $user;
        } else {
            return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {

        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");

        $stmt->bind_param("s", $email);

        if ($stmt->execute()) {
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();
            return $user;
        } else {
            return NULL;
        }
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");

        $stmt->bind_param("s", $email);

        $stmt->execute();

        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // user existed 
            $stmt->close();
            return true;
        } else {
            // user not existed
            $stmt->close();
            return false;
        }
    }

    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    public function hashSSHA($password) {

        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>

login.php

<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['email']) && isset($_POST['password'])) {

    // receiving the post params
    $email = $_POST['email'];
    $password = $_POST['password'];

    // get the user by email and password
    $user = $db->getUserByEmailAndPassword($email, $password);

    if ($user != false) {
        // use is found
        $response["error"] = FALSE;
        $response["uid"] = $user["unique_id"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
        echo json_encode($response);
    } else {
        // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error_msg"] = "Login credentials are wrong. Please try again!";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters email or password is missing!";
    echo json_encode($response);
}
?>
12
  • When you set your host to "localhost", MySQL tries to connect to the specified host with a UNIX socket file that, in your case, seems to be unavailable. Setting the DB_HOST const to 127.0.0.1 should make your day. Commented Feb 28, 2016 at 16:21
  • still getting same error. I am having this database and php files on a remote web hosting server. I think localhost and 127.0.0.1 mean the same thing 127.0.0.1 is the ip of localhost as generated by DNS Commented Feb 28, 2016 at 16:29
  • 1
    You need to post the code you're using to query with, since in testing what you posted did not generate any errors. Plus, seeing you're using AwardSpace, you need to change localhost to their servers ???.awardspace.net. They probably don't allow remote connections for free hosting, if that's what you're signed up with. Commented Feb 28, 2016 at 16:33
  • yes i signed for free hosting Commented Feb 28, 2016 at 16:37
  • 1
    and this function getUserByEmailAndPassword() no way to tell what that does. Sorry, but your question is unclear and missing too much code, the query, etc. Commented Feb 28, 2016 at 16:50

1 Answer 1

2

As I noted in comments:

AwardSpace sent you an email with the server address to use for db work.

The ??? as I pointed out are to be replaced with the fdbxxx (xxx are numbers) they gave you to use fdbxxx.awardspace.net.

  • This I know, because I use their service also.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.