
Originally Posted by
kuau
I haven't been on the forum for awhile. I had been checking something on php.net and noticed the warning about mysql_* functions being deprecated, so I came to DD & found traq's post. I am dumbfounded that I could not have heard anything about this until now. I didn't even start using php until about 2008. The host for all my client sites uses MySQL 5.5.32 so I fear that any day all my sites could suddenly stop working.
It's not happening immediately. Version 5.6 at the earliest (probably 5.7, or it might even wait until 6 because of how heavily it is used). And, regardless of *when* it is removed, you won't feel it until you upgrade PHP - e.g., ext/mysql will always be in version 5.5, no matter what.
The funny part is that ext/mysqli has been around since 2004, and has been the "recommended" (especially from within the mysql community) extension since its release.
But you're not alone in never having heard anything about it until now.
Other things to consider:

Originally Posted by
http://article.gmane.org/gmane.comp.php.devel/66894
Moving away from ext/mysql is not only about security but also about having access to all features of the MySQL database.
ext/mysql was built for MySQL 3.23 and only got very few additions since then while mostly keeping compatibility with this old version which makes the code a bit harder to maintain. From top of my head missing features not support be ext/mysql include:
* Stored Procedures (can't handle multiple result sets)
* Prepared Statements
* Encryption (SSL)
* Compression
* Full Charset support
* ...
So moving away from ext/mysql is a good thing.

Originally Posted by
kuau
I am far from a php expert, but it appears I have to rewrite the code for all my sites... a HUGE undertaking. I am wondering if it is just a case of doing a search-and-replace substituting mysqli for mysql everywhere it occurs. For example, here are the typical ways I use mysql...
You're right; it is a huge undertaking, especially if you have a large or diverse codebase. And while ext/mysqli is designed to be as familiar as possible to ext/mysql users, no, it's not a simple "search and replace" operation. Take your code, for example:
PHP Code:
<?php
/*
* $connection = mysql_connect($server,$user,$pass) or die("Couldn't connect to $db_name database.");
* $db = mysql_select_db($db_name, $connection) or die("Couldn't find $db_name database.");
*/
// setting up the initial connection is not that different.
// the main difference is that you'll be able to select the initial DB when you connect:
$connection = mysqli_connect( $server,$user,$pass,$db_name );
// error handling with die() will still work, obviously, but is not a preferred method (especially on "live" sites).
// but that's not really what we're talking about here. I won't harp on it.
/*
* $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
*/
// very similar, but the order of the arguments is reversed...
$result = mysqli_query( $connection,$sql );
// ... and **specifying the connection is no longer optional**
// (i.e., with ext/mysql, if you left out $connection it would assume you want the last-used connection.
// ext/mysqli _does not allow that_:
// mysqli_query( $sql ); // this emits a WARNING and then FAILS!)
/*
*while($var = mysql_fetch_assoc($result)){
* # . . .
*/
// very much the same.
while( $var = mysqli_fetch_assoc( $result ) ){
# . . .

Originally Posted by
kuau
If I change to mysqli in the connection string, does that mean the code will break anywhere I used mysql within that code set? ie. I will have to change ALL code all at once for an entire site or it won't work?
You can use both extensions in the same script, but they are not interoperable: mysqli can't use mysql connections, you can't switch extensions mid-code, and so forth. They are different functions and don't talk to each other.

Originally Posted by
kuau
Can I just add the "i" to mysql or are there other changes required to effect the update?
See above; there's more to it than just adding "i". All in all, however, it's not difficult: just tedious.
If everything had worked out as planned, then coders would have been trying out mysqli on new projects for some time now, and would be wholly comfortable with what changes would need to be made on their older projects. But we all know how that works (even assuming everyone had been successfully informed).
Of course, there is a lot of functionality that ext/mysqli offers that ext/mysql doesn't: prepared statements (**VERY** good for efficiency _and_ security), bound parameters (VERY good for efficiency/convenience), and object-oriented api* (plus a procedural api, very similar to the old ext/mysql)... lots of stuff. Check it out as you have the time.

Originally Posted by
kuau
Is mysql the only thing being deprecated?
See here. Short answer, no, but it's the only thing most people will notice.

Originally Posted by
kuau
I might as well do everything at once.
Recommendations:
- Start with a single project.
- Duplicate it somewhere (e.g., locally) so you can experiment without screwing anything up.
- Change only one statement at a time and test that the change was successful (your script should FAIL after that).
- If it works, change the next statement.
- When it seems to be going smoothly, change a whole sequence of statements at once.
- Repeat.
When you can do it comfortably, continue with your other projects.
---------------------------------------------------------------------------
* an object-oriented version of your example code: much easier/cleaner, IMO, but there's nothing wrong with the procedural approach at all.
PHP Code:
<?php
$connection = new mysqli( $server,$user,$pass,$db_name );
$result = $connection->query( $sql );
if( $result ){
while( $var = $result->fetch_assoc() ){
# . . .
}
}
Bookmarks