How to change mysql to mysqli?

前端 未结 11 1949
余生分开走
余生分开走 2020-11-21 04:55

Based on this code below I use for regular mysql, how could I convert it to use mysqli?

Is it as simple as changing mysql_query($sql); to

11条回答
  •  青春惊慌失措
    2020-11-21 05:29

    If you have a lot files to change in your projects you can create functions with the same names like mysql functions, and in the functions make the convert like this code:

    $sql_host =     "your host";      
    $sql_username = "username";    
    $sql_password = "password";       
    $sql_database = "database";       
    
    
    
    $mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database );
    
    
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    
    
    function mysql_query($query){
        $result = $mysqli->query($query);
        return $result;
    
    }
    
    function mysql_fetch_array($result){
        if($result){
            $row =  $result->fetch_assoc();
             return $row;
           }
    }
    
    function mysql_num_rows($result){
        if($result){
             $row_cnt = $result->num_rows;;
             return $row_cnt;
           }
    }
    

提交回复
热议问题