dongyu7074 2016-05-26 13:06
浏览 77
已采纳

从MySQL改为MySQLi,不再工作了

While changing mysql to mysqli I went from a completely working php file, to one thats not! I followed the instructions for every change, could you help? Here is my code before(mysql) and after (mysqli):

Before(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
    }
}
?>

After(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
    }
}
?>

The errors I get are:

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /something/home/something2/public_html/login.php on line 8

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known in /something/home/something2/public_html/login.php on line 8 Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known

( line 8 is: $con = mysqli_connect("$dtbhost","$dtbuser","$dtbpass","$dtbname"); )

Which then trigger more errors because $con is empty. Any help would be appreciated, thanks!

  • 写回答

1条回答 默认 最新

  • douyi1939 2016-05-26 13:14
    关注

    $dtbhost variable should be an IP address or domain name. For example: 'localhost', '127.0.0.1', 'example.com', etc.. Not a socket file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化