How to change mysql to mysqli?

前端 未结 11 1951
余生分开走
余生分开走 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: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 ;-)

提交回复
热议问题