使用php错误的MYSQL数据库连接

时间:2019-03-16 11:46:29

标签: php mysql database

我正在尝试连接到MySql数据库,但它给出了一个错误:

  

警告:mysqli_connect():php_network_getaddresses:getaddrinfo失败:此类主机未知。在第4行的C:\ xampp \ htdocs \ practice \ register.php中

     

警告:mysqli_connect():(HY000 / 2002):php_network_getaddresses:getaddrinfo失败:此类主机未知。在第4行的C:\ xampp \ htdocs \ practice \ register.php中

<?php       
session_start();        
$db = mysqli_connect('loacalhost', 'root', 'system', 'authentication');        
if (isset($_POST['register_btn'])) {
    session_start();    

    $username = mysql_real_escape_string($_POST['username']);     
    $email = mysql_real_escape_string($_POST['email']);    
    $password = mysql_real_escape_string($_POST['password']);     
    $password2 = mysql_real_escape_string($_POST['password2']);        

    if ($password == $password2) {     //create user   
        $password = md5($password); //hash for security purpose  
        $sql = "INSERT INTO users(username, email, password) 
            VALUES('$username', '$email', '$password')";

        mysqli_query($db,$sql);     
        $_SESSION['message' ] = "U R LOGGED IN";
        $_SESSION['usrname'] = $username;   
        header("location: home.php");
    } else {
        $_SESSION['message' ] = "the two passwords do not match";

    }   
}
?>

<!DOCTYPE html>
<html>
    <head>    
        <title>Register , login and logout user php mysql </title>
    </head>
    <body>
        <div class="header">
            <h1>Register , login and logout user php mysql</h1>
        </div>

        <form method="post" action="register.php>     
            <table>     
                <tr>    
                    <td> username: </td>    
                    <td> <input type="text" name="username" class="textInput"></td>   
                </tr>   
                <tr>    
                    <td>Email:</td>     
                    <td><input type="email" name="email" class="textInput"></td>     
                </tr>   
                <tr>
                    <td>password:</td>  
                    <td><input type="password" name="password" class="textInput"></td>   
                </tr>   
                <tr>    
                    <td>password again:</td>
                    <td><input type="password" name="password2" class="textInput"></td>
                </tr>   
                <tr>    
                    <td>username:</td>  
                    <td><input type="submit" name="register_btn" value="Register"></td>    
                </tr>   
            </table>
        </form>   
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

该行有错误

$db = mysqli_connect('loacalhost', 'root', 'system', 'authentication');        

localhost被写为loacalhost

所以实际代码应该是

$db = mysqli_connect('localhost', 'root', 'system', 'authentication');        

这将解决错误

  

警告:mysqli_connect():php_network_getaddresses:getaddrinfo失败:此类主机未知。在第4行的C:\ xampp \ htdocs \ practice \ register.php中

     

警告:mysqli_connect():(HY000 / 2002):php_network_getaddresses:getaddrinfo失败:此类主机未知。在第4行的C:\ xampp \ htdocs \ practice \ register.php中

答案 1 :(得分:-1)

可能您的mysqli_connect语法错误。

正确的语法例如:

mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

http://php.net/manual/fr/mysqli.construct.php