从MySQL改为MySQLi,不再工作了

时间:2016-05-26 13:06:54

标签: php mysql mysqli database-connection

在将mysql更改为mysqli时,我从一个完全正常工作的php文件转到了一个不是!我按照说明进行每次更改,你能帮忙吗?这是我的代码之前(mysql)和之后(mysqli):

在(MySQL的):

<?php

function db_open() {
    $dtbhost=":/something/home/something2/mysql/run/mysql.sock";
    $dtbuser="root";
    $dtbpass="mypass";
    $dtbname="mydb";
    $connection   =   mysql_connect("$dtbhost",   "$dtbuser",   "$dtbpass") 
        or die('00: No Connection');
    @ mysql_select_db("$dtbname") or die('01: No Database');      
}

session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        echo ("<SCRIPT LANGUAGE='JavaScript'> 
            window.alert('You did not complete all of the required fields') 
            window.location.href='index.html' 
            </SCRIPT>");
    }
    else
    {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    db_open(); 
    // To protect MySQL injection for Security purpose
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    $query = mysql_query("SELECT * FROM login_users WHERE username = '$username' AND password = '$password'") 
            or die(mysql_error());

    $rows = mysql_num_rows($query);
    if ($rows == 1) {
        $_SESSION['login_user']=$username; // Initializing Session
        header("location: profile.html"); // Redirecting To Other Page

    } else {
        echo ("<SCRIPT LANGUAGE='JavaScript'> 
            window.alert('Wrong username password combination. Please re-enter.') 
            window.location.href='index.html' 
            </SCRIPT>");
    }
    mysql_close($connection); // Closing Connection
    }
}
?>

在(mysqli的):

<?php

function db_open() {
    $dtbhost=":/something/home/something2/mysql/run/mysql.sock";
    $dtbuser="root";
    $dtbpass="mypass";
    $dtbname="mydb";
    $con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");

    // Check connection
    if (mysqli_connect_errno())
      {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
}

session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        echo ("<SCRIPT LANGUAGE='JavaScript'> 
            window.alert('You did not complete all of the required fields') 
            window.location.href='index.html' 
            </SCRIPT>");
    }
    else
    {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    db_open(); 
    // To protect MySQL injection for Security purpose
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysqli_real_escape_string($con, $username);
    $password = mysqli_real_escape_string($con, $password);

    $query = mysqli_query($con, "SELECT * FROM login_users WHERE username = '$username' AND password = '$password'") 
            or die(mysqli_error());

    $rows = mysqli_num_rows($query);
    if ($rows == 1) {
        $_SESSION['login_user']=$username; // Initializing Session
        header("location: profile.html"); // Redirecting To Other Page

    } else {
        echo ("<SCRIPT LANGUAGE='JavaScript'> 
            window.alert('Wrong username password combination. Please re-enter.') 
            window.location.href='index.html' 
            </SCRIPT>");
    }
    mysqli_close($con); // Closing Connection
    }
}
?>

我得到的错误是:

  

警告:mysqli_connect():php_network_getaddresses:getaddrinfo失败:提供主机名或服务名,或者在第8行的/something/home/something2/public_html/login.php中未知

     

警告:mysqli_connect():( HY000 / 2002):php_network_getaddresses:   getaddrinfo失败:提供主机名或服务名,或者未知   第8行/something/home/something2/public_html/login.php未能   连接MySQL:php_network_getaddresses:getaddrinfo失败:   提供的主机名或服务名称,或未知的

(第8行是:$con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname");

然后触发更多错误,因为$ con为空。 任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

$ dtbhost变量应该是IP地址或域名。例如:'localhost','127.0.0.1','example.com'等。不是套接字文件。