How to change mysql to mysqli?

前端 未结 11 1904
余生分开走
余生分开走 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:34

    I have just created the function with the same names to convert and overwrite to the new one php7:

    $host =     "your host";      
    $un = "username";    
    $pw = "password";       
    $db = "database"; 
    
    $MYSQLI_CONNECT = mysqli_connect($host, $un, $pw, $db);
    
    function mysql_query($q) {
        global $MYSQLI_CONNECT;
        return mysqli_query($MYSQLI_CONNECT,$q);
    }
    
    function mysql_fetch_assoc($q) {
        return mysqli_fetch_assoc($q);
    }
    
    function mysql_fetch_array($q){
        return mysqli_fetch_array($q , MYSQLI_BOTH);
    }
    
    function mysql_num_rows($q){
        return mysqli_num_rows($q);
    }
    
    function mysql_insert_id() {
        global $MYSQLI_CONNECT;
        return mysqli_insert_id($MYSQLI_CONNECT);
    }
    
    function mysql_real_escape_string($q) {
        global $MYSQLI_CONNECT;
        return mysqli_real_escape_string($MYSQLI_CONNECT,$q);
    }
    

    It works for me , I hope it will work for you all , if I mistaken , correct me.

提交回复
热议问题