Mysql数据库错误连接

时间:2017-12-09 09:25:19

标签: php mysql

可能听起来很明显但我的数据库没有连接到我的phpmyadmin。

我做错了吗?我使用mamp作为php使用的后端服务器。

 <?php
    //
    require('database.php');
    $connection = mysqli_connect('localhost', 'root','','blurt');
    // If the values are posted, insert them into the database.
    if (isset($_POST['Register'])){
        $username = $_POST['username'];
    $email = $_POST['email'];
        $password = $_POST['password'];

        $query = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')";
         //echo $query;
        $result=mysqli_query($connection,$query);
        if($result){
            $smsg = "User Created Successfully.";
        }else{
            $fmsg ="User Registration Failed";
        }
    }

    $connection = mysqli_connect('localhost', 'root', '', 'blurt');
    if (!$connection) {
        die("Database Connection Failed" . mysqli_error($connection));
    } $select_db = mysqli_select_db($connection, 'blurt');
    if (!$select_db) {
        die("Database Selection Failed" . mysqli_error($connection));
    }
    ?>

3 个答案:

答案 0 :(得分:0)

通过查看您的问题,看起来您在查询后注册了连接..

请尝试更改订单..

在使用之前必须创建

$ connection ..

如下所示:

// Step -1
$connection = mysqli_connect('localhost', 'root', '', 'blurt');
if (!$connection) {
die("Database Connection Failed" . mysqli_error($connection));
} $select_db = mysqli_select_db($connection, 'blurt');
if (!$select_db) {
die("Database Selection Failed" . mysqli_error($connection));
}

//Step-2
$query = "INSERT INTO `user` (username, email, password) VALUES ('$username', '$email', '$password')";
 //echo $query;
$result=mysqli_query($connection,$query);
if($result){
    $smsg = "User Created Successfully.";
}else{
    $fmsg ="User Registration Failed";
}

答案 1 :(得分:0)

    $connection = mysqli_connect('localhost', 'root', '', 'dbname');
if (!$connection) {
    die("Database Connection Failed" . mysqli_error($connection));
} 

$select_db = mysqli_select_db($connection, 'dbname');
if (!$select_db) {
    die("Database Selection Failed" . mysqli_error($connection));
}
 $query = "INSERT INTO `user` ('$username', '$email', '$password') VALUES ('username', 'email', 'password')";
     //echo $query;
    $result=mysqli_query($connection,$query);
    if($result){
        $smsg = "User Created Successfully.";
    }else{
        $fmsg ="User Registration Failed";
    }

答案 2 :(得分:0)

解决方案非常样本

此处:http://curs.io/KN97HAOB