Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating from MYSQL to MYSQLI [duplicate]

Tags:

php

mysql

mysqli

So as MYSQL is deprecated and eveyone keeps telling me to update, I thought it was about time I did.

But as I'm not used to mysqli_*, it seems alien to me. And it's not a simple edit when I have a whole site coded in Mysql.

So I'm wondering: How would I transform the below code into Mysqli? Just to give me and anyone else a good starting point when dealing with querying the database.

$sql_follows="SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2";
$query_follows=mysql_query($sql_follows) or die("Error finding friendships");
if($query_follows>0){
}

EDIT: On reading up and editing my whole site the above code converted to MYSQLI_ would go something like this..

 $Your_SQL_query_variable= mysqli_query($connectionvariable,"SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2")) {
        printf("Error: %s\n", $mysqli->error);
    }
like image 985
dave Avatar asked Aug 18 '12 16:08

dave


People also ask

How do I switch from MySQL to MySQLi?

If you want to convert your script from a MySQL extension to a MySQLi extension manually, you must start with the top of the script and start converting all the methods one by one. For this, open the script in any text editor and use the find-and-replace tool to replace mysql_ with mysqli .

Why we use MySQLi instead of MySQL?

MySQLi supports both procedural interfaces and object oriented interfaces while MySQL supports only procedural interfaces. MySQLi supports stored procedure but MySQL does not. There is enhanced security and improved debugging features in MySQLi where this is comparatively lagging in MySQL.

Is MySQLi faster than MySQL?

The MySQL extension is very slightly faster than MySQLi in most benchmarks I've seen reported. The difference is so slight, however, that this should probably not be your criterion for deciding between the two. Other factors dwarf the difference in performance between mysql and mysqli.

How can I convert MySQL database to MySQLi in PHP?

To migrate it to MySQLi, we use the mysqli_select_db method to select the database and then the mysqli_query method to run the query and return the result. This statement is replaced with the mysqli_query method using the DROP DATABASE sql... This is a simple name change.


2 Answers

You can download a converter tool from here:

https://github.com/philip/MySQLConverterTool

The code it generates is pretty gross, mainly because of the way it implements the default database link argument with a $GLOBAL variable. (This also makes it easy to recognize when someone is using code that's gone through the converter.)

There's also a MySQL Shim Library located here:

https://github.com/dshafik/php7-mysql-shim

like image 104
Barmar Avatar answered Oct 30 '22 14:10

Barmar


The best place to transform is to look at the php reference library. Very easy to use and will tell you everything you need to know:

http://www.php.net/manual/en/mysqli.query.php

like image 33
Conrad Lotz Avatar answered Oct 30 '22 13:10

Conrad Lotz