How to change mysql to mysqli?

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

    Here is a complete tutorial how to make it quickly if you need to make worgking again a website after PHP upgrade. I used it after upgrading hosting for my customers from 5.4 (OMG!!!) to 7.x PHP version.

    This is a workaround and it is better to rewrite all code using PDO or mysqli Class.

    1. Connection definition

    First of all, you need to put the connection to a new variable $link or $con, or whatever you want.

    Example

    Change the connection from :

    @mysql_connect($host, $username, $password) or die("Error message...");
    @mysql_select_db($db);
    

    or

    @mysql_connect($host, $username, $password, $db) or die("Error message...");
    

    to:

    $con = mysqli_connect($host, $username, $password, $db) or die("Error message...");
    

    2. mysql_* modification

    With Notepad++ I use "Find in files" (Ctrl + Shift + f) :

    in the following order I choose "Replace in Files" :

    1. mysql_query( -> mysqli_query($con,

    2. mysql_error() -> mysqli_error($con)

    3. mysql_close() -> mysqli_close($con)

    4. mysql_insert_id() -> mysqli_insert_id($con)

    5. mysql_real_escape_string( -> mysqli_real_escape_string($con,

    6. mysql_ -> mysqli_

    3. adjustments

    if you get errors it is maybe because your $con is not accessible from your functions.

    You need to add a global $con; in all your functions, for example :

    function my_function(...) {
        global $con;
        ...
    }
    

    In SQL class, you will put connection to $this->con instead of $con. and replace it in each functions call (for example : mysqli_query($con, $query);)

    0 讨论(0)
  • 2020-11-21 05:26

    The easiest way i always handle this Where

     $con = mysqli_connect($serverName,$dbusername,$dbpassword);
    

    3 steps replacement in the following order

    1. All "mysql_select_db(" with "mysqli_select_db($con,"
    2. All "mysql_query(" with "mysqli_query($con," and
    3. All "mysql_" with "mysqli_".

    This works for me everytime

    0 讨论(0)
  • 2020-11-21 05:27

    The first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.

    To help with that, the MySQLi Extension Function Summary is definitely something that will prove helpful.

    For instance:

    • mysql_connect will be replaced by mysqli_connect
    • mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
    • mysql_query will be replaced by mysqli_query
    • and so on

    Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)

    For instance:

    • with mysql, you have to use the mysql_select_db once connected, to indicate on which database you want to do your queries
    • mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect.
    • Still, there is also a mysqli_select_db function that you can use, if you prefer.

    Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)
    0 讨论(0)
  • 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;
           }
    }
    
    0 讨论(0)
  • 2020-11-21 05:34

    In case of big projects, many files to change and also if the previous project version of PHP was 5.6 and the new one is 7.1, you can create a new file sql.php and include it in the header or somewhere you use it all the time and needs sql connection. For example:

    //local
    $sql_host =     "localhost";      
    $sql_username = "root";    
    $sql_password = "";       
    $sql_database = "db"; 
    
    
    $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();
    }
    
    // /* change character set to utf8 */
    if (!$mysqli->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
        exit();
    } else {
        // printf("Current character set: %s\n", $mysqli->character_set_name());
    }
    if (!function_exists('mysql_real_escape_string')) {
        function mysql_real_escape_string($string){
            global $mysqli;
            if($string){
                // $mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database );            
                $newString =  $mysqli->real_escape_string($string);
                return $newString;
            }
        }
    }
    // $mysqli->close();
    $conn = null;
    if (!function_exists('mysql_query')) {
        function mysql_query($query) {
            global $mysqli;
            // echo "DAAAAA";
            if($query) {
                $result = $mysqli->query($query);
                return $result;
            }
        }
    }
    else {
        $conn=mysql_connect($sql_host,$sql_username, $sql_password);
        mysql_set_charset("utf8", $conn);
        mysql_select_db($sql_database);
    }
    
    if (!function_exists('mysql_fetch_array')) {
        function mysql_fetch_array($result){
            if($result){
                $row =  $result->fetch_assoc();
                return $row;
            }
        }
    }
    
    if (!function_exists('mysql_num_rows')) {
        function mysql_num_rows($result){
            if($result){
                $row_cnt = $result->num_rows;;
                return $row_cnt;
            }
        }
    }
    
    if (!function_exists('mysql_free_result')) {
        function mysql_free_result($result){
            if($result){
                global $mysqli;
                $result->free();
    
            }
        }
    }
    
    if (!function_exists('mysql_data_seek')) {
        function mysql_data_seek($result, $offset){
            if($result){
                global $mysqli;
                return $result->data_seek($offset);
    
            }
        }
    }
    
    if (!function_exists('mysql_close')) {
        function mysql_close(){
            global $mysqli;
            return $mysqli->close();
        }
    }
    
    if (!function_exists('mysql_insert_id')) {
        function mysql_insert_id(){
                global $mysqli;
                $lastInsertId = $mysqli->insert_id;
                return $lastInsertId;
        }
    }
    
    if (!function_exists('mysql_error')) {
        function mysql_error(){
            global $mysqli;
            $error = $mysqli->error;
            return $error;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:34

    I would tentatively recommend using PDO for your SQL access.

    Then it is only a case of changing the driver and ensuring the SQL works on the new backend. In theory. Data migration is a different issue.

    Abstract database access is great.

    0 讨论(0)
提交回复
热议问题