Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find/replace mysql_ to mysqli_ makes code nonfunctional

Tags:

php

mysql

mysqli

So as I'm sure anyone who's been a regular on SO has noticed, the mysql_ functions are going to be deprecated and it is suggested that one use mysqli_ or PDO instead. Thus, I decided to transition over to mysqli by doing a simple find/replace in my code, replacing every mysql_ with mysqli_. This seemed to work okay in Dreamweaver, and I got no syntax errors or anything. All the new mysqli_ functions were colored blue, meaning that they were recognized as valid functions.

However, when I saved everything and ran the code, I noticed that any code having to do with mysql had become nonfunctional. Undoing the replace solved the problem.

Is my server perhaps not supporting the mysqli functions? I know that it's running php 5 and mysql 5. Is there perhaps something else that you have to add to the code? What am I missing?

like image 798
Charles Avatar asked Jan 19 '13 19:01

Charles


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 .

Can I use both MySQL and MySQLi?

It is possible to include both MySQL and MySQLi when connecting to a single database, but it is incredibly delicate and with large amounts of data being passed through it can get very messy and hard to control. it is best to use MySQLi in general in my opinion because it is much more secure and up to date.

Is MySQLi same as MySQL?

What is the difference between mysql and mysqli? Basically, MySQL is the old database driver, and MySQLi is the Improved driver. The "i" stands for "improved" so it is MySQL improved. MySQLi can be done procedural and object-oriented whereas MySQL can only be used procedurally.

Is MySQLi deprecated in PHP 7?

The oldest one uses the MySQL extension, which was deprecated as of PHP 5.5 and fully removed in PHP 7. The mysql() function no longer works in PHP 7.


1 Answers

It's a good time to switch now, since PHP 7.0 removed the ext/mysql functions from core.

Consider the following legacy code

$res = mysql_query($sql);

The mysql_ functions were lazy, in that it would use whatever connection was open. But mysqli_ is not only not lazy, it's an object, not a connection resource (which is what mysql_connect() would return). Look at these lines here for connecting to the database

$mysqli = new mysqli('host', 'username', 'password', 'db');
$mysqli = mysqli_connect('host', 'username', 'password', 'db');

Both of them give you the same thing...

Returns an object which represents the connection to a MySQL Server.

Yes, that's right. Even the procedural connection function returns an object. The simplest way to use it is directly in is object form

$mysqli->query($sql);

The procedural functions are simply wrappers for this. But they are NOT lazy. Check out the difference

mysqli_query($mysqli, $sql);

The single largest gotcha in this (why a simple find and replace of mysql_ with mysqli_ fails) is that most of the mysqli_ functions require you to pass the connection object as the first argument. It's also important to note that results are objects as well (mysqli_result class), although their procedural counterparts only require the result set so you can simply find/replace those.

Just to be complete, there's one other gotcha that's less common (this is really old code): mysql_result. This function has no equivalent in mysqli_. You should switch to the full row return functions like mysqli_fetch_assoc($result)

like image 142
Machavity Avatar answered Sep 30 '22 13:09

Machavity