简体   繁体   English

ADODB - 从 Mysql 切换到 Mysqli

[英]ADODB - Switch from Mysql to Mysqli

In the light of MySQL to soon be deprecated, I need to move a large website using ADODB from MySQL to MySQLi.鉴于 MySQL 即将被弃用,我需要将一个使用 ADODB 的大型网站从 MySQL 迁移到 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.现在我在 Stackoverflow 上查找了一些主题,感谢社区,我已经对需要做的事情有了一个大致的了解。 Best topics on the matter are those ones:关于这个问题的最佳主题是那些:

ADODB mySQLi Connection ADODB mySQLi 连接

Switch large website from MySQL to MySQLi 将大型网站从 MySQL 切换到 MySQLi

However, I do still need a bit more clarification on my particular case, where ADODB is being used.但是,我仍然需要对使用 ADODB 的特定案例进行更多说明。

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?现在,由于我正在使用 ADODB,我是否还需要将 MySQL_* 函数的所有实例更改为 MySQLi_* 或 ADODB 会自动处理此问题? I think I know the answer but anyhow have to ask.我想我知道答案,但无论如何必须问。

My most common MySQL_ functions are:我最常见的 MySQL_ 函数是:

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();最常见的用法是$variable = mysql_insert_id(); or $v1 = mysql_query($v);$v1 = mysql_query($v);

Is there anything else I should take into consideration when moving from MySQL to MySQLi for ADODB?从 MySQL 迁移到 MySQLi for ADODB 时,还有什么我应该考虑的吗?

"do I also need to change all instances of MySQL_* functions to MySQLi_* ?" “我还需要将 MySQL_* 函数的所有实例更改为 MySQLi_* 吗?”

The answer is yes.答案是肯定的。 Different MySQL APIs/functions do not intermix.不同的 MySQL API/函数不会混合使用。 You must use the same API/functions from connection to querying.从连接到查询,您必须使用相同的 API/函数。

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.您可以使用以下函数,只需将mysql_替换为mysqli_ ,同时在需要它的函数中传递数据库连接并将其作为第一个参数。

  • Ie mysqli_query($connection, $query) .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() - *

1) I would suggest you to use pdo_mysql instead of mysql since it has better support for transactions. 1) 我建议你使用 pdo_mysql 而不是 mysql,因为它对事务有更好的支持。

2) ADODB initializes the driver being used (eg mysql or pdo_mysql) by using the dsn identifier from the connection string ie pdo_mysql://localhost/mydb or mysql://localhost/mydb. 2) ADODB 通过使用连接字符串中的 dsn 标识符(即 pdo_mysql://localhost/mydb 或 mysql://localhost/mydb)初始化正在使用的驱动程序(例如 mysql 或 pdo_mysql)。

Just switch mysql -> pdo_mysql in the connection string and it starts to use mysqli -driver instead.只需在连接字符串中切换 mysql -> pdo_mysql ,它就会开始使用 mysqli -driver。

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. 3)驱动加载逻辑位于adodb.inc.php的~4860行,至少在我使用的composer版本中,没有配置mysqli的开关,但是会自动升级mysql->mysqli如果您有 PHP 7.0.0 或更高版本。 You could tweak this code to force mysqli also by adding one if-statement, but then you have a custom fork of ADODB.您也可以通过添加一个 if 语句来调整此代码以强制使用 mysqli,但是您有一个自定义的 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM