duanan1946 2016-07-19 11:55
浏览 308
已采纳

数据库连接错误:无法通过套接字连接到本地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>
  • 写回答

1条回答 默认 最新

  • duanbianweng5353 2016-07-19 11:58
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名