数据库连接错误:无法通过套接字连接到本地MySQL服务器[重复]

数据库连接错误:无法通过套接字连接到本地MySQL服务器[重复]

问题描述:

I have realized the database on a hosting website and not on the mac using MySQL. I use the database to SignUp/Login in an IOS app. The database is using a PHP code for connecting.

This is the PHP code:

<?php

header('Content-type: application/json');
if($_POST) {
    $Nome   = $_POST['nome'];
    $Cognome   = $_POST['cognome'];
    $Email   = $_POST['email'];
    $DOB   = $_POST['dob'];
    $password   = $_POST['password'];
    $c_password = $_POST['c_password'];

    if($_POST['email']) {
        if ( $password == $c_password ) {

            $db_name     = '****************';
            $db_user     = '****************';
            $db_password = '*********************';
            $server_url  = 'localhost';

            $mysqli = new mysqli('localhost', $db_user, $db_name, $db_password);


            if (mysqli_connect_errno()) {
                error_log("Connect failed: " . mysqli_connect_error());
                echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
            } else {
                $stmt = $mysqli->prepare("INSERT INTO Users (nome, cognome, email, dob, password, c_password) VALUES (?, ?, ?, ?, ?, ?)");
                $password = md5($password);
                $stmt->bind_param('ssssss', $nome, $cognome, $email, $dob, $password, $c_password);


                $stmt->execute();

                if ($stmt->error) {error_log("Error: " . $stmt->error); }

                $success = $stmt->affected_rows;


                $stmt->close();


                $mysqli->close();
                error_log("Success: $success");

                if ($success > 0) {
                    error_log("User '$email' created.");
                    echo '{"success":1}';
                } else {
                    echo '{"success":0,"error_message":"Email Exist."}';
                }
            }
        } else {
            echo '{"success":0,"error_message":"Passwords does not match."}';
        }
    } else {
        echo '{"success":0,"error_message":"Invalid Email."}';
    }
}else {
    echo '{"success":0,"error_message":"Invalid Data."}';
}
?>

This is the error that I see when I try my database:

(HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /srv/disk8/2129310/www/haer2jkqlgjql.co.nf/signUp.php on line 20

This error tells me that there are errors or there is an error in line 20, but I don't understand the reason.

</div>

Its syntax mistake

$mysqli = new mysqli('localhost', $db_user, $db_name, $db_password);

Right syntax is

$mysqli = new mysqli('localhost',$db_user,$db_password, $db_name);