2

In the light of MySQL to soon be deprecated, I need to move a large website using ADODB from MySQL to MySQLi.

Now I have looked up a few topics on Stackoverflow and thanks to the community I already have a genral idea of what needs to be done. Best topics on the matter are those ones:

ADODB mySQLi Connection

Switch large website from MySQL to MySQLi

However, I do still need a bit more clarification on my particular case, where ADODB is being used.

This is what I use to connect to the DB:

define('DBHOST', 'db_host');
define('DBUSER', 'db_user'); 
define('DBPASS', 'db_pass');    
define('DBNAME', 'db_name'); 

include('adodb/adodb.inc.php');
$db = ADONewConnection('mysql');
$db->Connect(DBHOST,DBUSER,DBPASS,DBNAME) or die("Database not found!");

So first I am changing:

$db = ADONewConnection('mysql');

to

$db = ADONewConnection('mysqli');

That's the easy part, I guess.

Now since I am using ADODB, do I also need to change all instances of MySQL_* functions to MySQLi_* or ADODB takes care of this automatically? I think I know the answer but anyhow have to ask.

My most common MySQL_ functions are:

mysql_insert_id()
mysql_query()
mysql_fetch_array()
mysql_num_rows()
mysql_escape_string()
mysql_connect()
mysql_select_db()
mysql_error()

Most common usage is like $variable = mysql_insert_id(); or $v1 = mysql_query($v);

Is there anything else I should take into consideration when moving from MySQL to MySQLi for ADODB?

Community
  • 1
  • 1
user3132858
  • 609
  • 1
  • 11
  • 27

2 Answers2

4

"do I also need to change all instances of MySQL_* functions to MySQLi_* ?"

The answer is yes. Different MySQL APIs/functions do not intermix. You must use the same API/functions from connection to querying.

You can use the following functions, simply replacing mysql_ by mysqli_, while passing a database connection in functions that require it and as the first parameter.

  • I.e. mysqli_query($connection, $query).

They are marked with asterisks *.

mysqli_insert_id() - *
mysqli_query() - *
mysqli_fetch_array()
mysqli_num_rows()
mysqli_escape_string() - *
mysqli_connect() - *
mysqli_select_db() - *
mysqli_error() - *
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Harry
  • 126
  • 4
  • *...while passing db connection to functions that require it.* – Funk Forty Niner Jul 30 '15 at 11:11
  • Thanks for the answer, I will do as you suggest. – user3132858 Jul 31 '15 at 09:11
  • Isn't it correct to assume, the methods from ADODB should be used rather than using the direct Mysqli queries. @user3132858 : To answer your query "do I also need to change all instances of MySQL_* functions to MySQLi_* or ADODB takes care of this automatically" The answer is YES, ADO takes care of it automatically. it switches and includes the required driver as passed while connecting. – Nishant Shrivastava May 08 '20 at 06:24
3

1) I would suggest you to use pdo_mysql instead of mysql since it has better support for transactions.

2) ADODB initializes the driver being used (e.g. mysql or pdo_mysql) by using the dsn identifier from the connection string i.e. pdo_mysql://localhost/mydb or mysql://localhost/mydb.

Just switch mysql -> pdo_mysql in the connection string and it starts to use mysqli -driver instead.

3) Driver loading logic is located on row ~4860 at adodb.inc.php, and at least in the version I'm using from composer, there is no switch to be able to configure mysqli, but it upgrades mysql -> mysqli automatically if you have PHP 7.0.0 or higher. You could tweak this code to force mysqli also by adding one if-statement, but then you have a custom fork of ADODB. Maybe do a pull request.

        case 'mysql':
            // mysql driver deprecated since 5.5, removed in 7.0
            // automatically switch to mysqli
            if(version_compare(PHP_VERSION, '7.0.0', '>=')) {
                $db = 'mysqli';
            }
            $class = $db;
            break;
PHZ.fi-Pharazon
  • 1,479
  • 14
  • 15
  • Thank you! This is the very solution that helps me update the code cleanly. The up-to-date adodb-5.20.15 only added support for more db's, not upgrade mysql to mysqli. Your patch does the trick. I'm now working on other problems resulted from features removed by PHP7.0. – Charles Jie Dec 16 '19 at 13:05
  • Apt, and should be the accepted answer. A way cleaner way to solve the stated problem. – Nishant Shrivastava May 08 '20 at 06:28