php 如何将mysql更改为mysqli?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1390607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change mysql to mysqli?
提问by JasonDavis
Based on this code below I use for regular mysql, how could I convert it to use mysqli?
根据下面我用于常规 mysql 的代码,我如何将其转换为使用 mysqli?
Is it as simple as changing mysql_query($sql);to mysqli_query($sql);?
它是那样简单改变mysql_query($sql);来mysqli_query($sql);?
<?PHP
//in my header file that is included on every page I have this
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>");
mysql_select_db($DB['dbName']);
// end header connection part
// function from a functions file that I run a mysql query through in any page.
function executeQuery($sql) {
$result = mysql_query($sql);
if (mysql_error()) {
$error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
if ($_SESSION['auto_id'] == 1) {
$sql_formatted = highlight_string(stripslashes($sql), true);
$error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
}
die($error);
}
return $result;
}
// example query ran on anypage of the site using executeQuery function
$sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id'];
$result_member=executequery($sql);
if($line_member=mysql_fetch_array($result_member)){
extract($line_member);
} else {
header("location: index.php");
exit;
}
?>
回答by Pascal MARTIN
The first thing to do would probably be to replace every mysql_*function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.
要做的第一件事可能是mysql_*用等效的替换每个函数调用mysqli_*,至少如果您愿意使用过程 API —— 这将是更简单的方法,考虑到您已经有一些基于 MySQL API 的代码,是程序性的。
To help with that, the The MySQLi Extension Function Summaryis definitely something that will prove helpful.
为了帮助解决这个问题,MySQLi 扩展功能摘要绝对会被证明是有帮助的。
For instance:
例如:
mysql_connectwill be replaced bymysqli_connectmysql_errorwill be replaced bymysqli_errorand/ormysqli_connect_error, depending on the contextmysql_querywill be replaced bymysqli_query- and so on
mysql_connect将被替换mysqli_connectmysql_error将被mysqli_error和/或替换mysqli_connect_error,具体取决于上下文mysql_query将被替换mysqli_query- 等等
Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
注意:对于某些函数,您可能需要仔细检查参数:也许这里和那里有一些差异——但不是那么多,我想说的是:mysql 和 mysqli 都基于同一个库(libmysql;至少对于 PHP <= 5.2)
For instance:
例如:
- with mysql, you have to use the
mysql_select_dbonce connected, to indicate on which database you want to do your queries - mysqli, on the other side, allows you to specify that database name as the fourth parameter to
mysqli_connect. - Still, there is also a
mysqli_select_dbfunction that you can use, if you prefer.
- 使用 mysql,您必须使用
mysql_select_db一旦连接,来指示您要在哪个数据库上进行查询 - 另一方面,mysqli 允许您将该数据库名称指定为
mysqli_connect. - 不过,
mysqli_select_db如果您愿意,也可以使用一个功能。
Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)
完成后,尝试执行新版本的脚本...并检查是否一切正常;如果没有......是时候寻找错误了;-)
回答by dhw
(I realise this is old, but it still comes up...)
(我意识到这是旧的,但它仍然出现......)
If you do replace mysql_*with mysqli_*then bear in mind that a whole load of mysqli_*functions need the database link to be passed.
如果您确实替换mysql_*为mysqli_*,请记住,一整套mysqli_*函数都需要传递数据库链接。
E.g.:
例如:
mysql_query($query)
becomes
变成
mysqli_query($link, $query)
I.e., lots of checking required.
即,需要进行大量检查。
回答by Aduragbemi Ogundijo
The easiest way i always handle this Where
我总是处理这个最简单的方法在哪里
$con = mysqli_connect($serverName,$dbusername,$dbpassword);
3 steps replacement in the following order
按以下顺序3步更换
- All "mysql_select_db(" with "mysqli_select_db($con,"
- All "mysql_query(" with "mysqli_query($con," and
- All "mysql_" with "mysqli_".
- 所有“ mysql_select_db(”和“ mysqli_select_db($con,”)
- 所有“ mysql_query(”与“ mysqli_query($con,”和
- 所有“ mysql_”和“ mysqli_”。
This works for me everytime
这每次都对我有用
回答by Tomá? Votruba
2020+ Answer
2020+ 答案
I've created a tool called Rector, that handles instant upgrades. There is also mysql → mysqliset.
我创建了一个名为 Rector的工具,用于处理即时升级。还有mysql → mysqliset。
It handles:
它处理:
- function renaming
- constant renaming
- switched arguments
non-1:1 function calls changes, e.g.
$data = mysql_db_name($result, $row);↓
mysqli_data_seek($result, $row); $fetch = mysql_fetch_row($result); $data = $fetch[0];
- 函数重命名
- 不断重命名
- 切换参数
非 1:1 函数调用更改,例如
$data = mysql_db_name($result, $row);↓
mysqli_data_seek($result, $row); $fetch = mysql_fetch_row($result); $data = $fetch[0];
Just use it on your code:
只需在您的代码中使用它:
composer require rector/rector --dev
vendor/bin/rector process src --set mysql-to-mysqli
I've already run it on 2 big PHP projects and it works perfectly.
我已经在 2 个大型 PHP 项目上运行过它,并且运行良好。
回答by Dharman
The ultimate guide to upgrading mysql_*functions to MySQLi API
将mysql_*功能升级到 MySQLi API的终极指南
The reason for the new mysqli extension was to take advantage of new features found in MySQL systems versions 4.1.3 and newer. When changing your existing code from mysql_*to mysqli API you should avail of these improvements, otherwise your upgrade efforts could go in vain.
The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
新的 mysqli 扩展的原因是利用 MySQL 系统版本 4.1.3 和更新版本中的新功能。将现有代码从mysql_*mysqli API更改为 mysqli API 时,您应该利用这些改进,否则您的升级工作可能会白费。
mysqli 扩展有很多好处,对 mysql 扩展的主要增强是:
- Object-oriented interface
- Support for Prepared Statements
- Enhanced debugging capabilities
- 面向对象的接口
- 支持准备好的报表
- 增强的调试功能
When upgrading from mysql_*functions to MySQLi, it is important to take these features into consideration, as well as some changes in the way this API should be used.
当从mysql_*函数升级到 MySQLi 时,重要的是要考虑这些特性,以及这个 API 应该使用的方式的一些变化。
1. Object-oriented interface versus procedural functions.
1. 面向对象的接口与过程函数。
The new mysqli object-oriented interface is a big improvement over the older functions and it can make your code cleaner and less susceptible to typographical errors. There is also the procedural version of this API, but its use is discouraged as it leads to less readable code, which is more prone to errors.
新的 mysqli 面向对象接口是对旧函数的重大改进,它可以使您的代码更干净,并且不易受排版错误的影响。还有这个 API 的过程版本,但不鼓励使用它,因为它会导致代码可读性降低,更容易出错。
To open new connection to the database with MySQLi you need to create new instance of MySQLi class.
要使用 MySQLi 打开与数据库的新连接,您需要创建 MySQLi 类的新实例。
$mysqli = new \mysqli($host, $user, $password, $dbName);
$mysqli->set_charset('utf8mb4');
Using procedural style it would look like this:
使用程序风格,它看起来像这样:
$mysqli = mysqli_connect($host, $user, $password, $dbName);
mysqli_set_charset($mysqli, 'utf8mb4');
Keep in mind that only the first 3 parameters are the same as in mysql_connect. The same code in the old API would be:
请记住,只有前 3 个参数与mysql_connect. 旧 API 中的相同代码是:
$link = mysql_connect($host, $user, $password);
mysql_select_db($dbName, $link);
mysql_query('SET NAMES utf8');
If your PHP code relied on implicit connection with default parameters defined in php.ini, you now have to open the MySQLi connection passing the parameters in your code, and then provide the connection link to all procedural functions or use the OOP style.
如果您的 PHP 代码依赖于 php.ini 中定义的默认参数的隐式连接,则您现在必须打开 MySQLi 连接并在您的代码中传递参数,然后提供所有过程函数的连接链接或使用 OOP 样式。
For more information see the article: How to connect properly using mysqli
有关更多信息,请参阅文章:如何使用 mysqli 正确连接
2. Support for Prepared Statements
2. 支持准备好的报表
This is a big one. MySQL has added support for native prepared statements in MySQL 4.1 (2004). Prepared statements are the best way to prevent SQL injection. It was only logical that support for native prepared statements was added to PHP. Prepared statements should be used whenever data needs to be passed along with the SQL statement (i.e. WHERE, INSERTor UPDATEare the usual use cases).
这是一个很大的。MySQL 在 MySQL 4.1 (2004) 中添加了对原生预准备语句的支持。准备好的语句是防止 SQL 注入的最好方法。对原生准备好的语句的支持被添加到 PHP 中是合乎逻辑的。只要数据需要与 SQL 语句一起传递(即WHERE,INSERT或者UPDATE是通常的用例),就应该使用准备好的语句。
The old MySQL API had a function to escape the strings used in SQL called mysql_real_escape_string, but it was never intended for protection against SQL injectionsand naturally shouldn't be used for the purpose.
The new MySQLi API offers a substitute function mysqli_real_escape_stringfor backwards compatibility, which suffers from the same problems as the old one and therefore should not be used unless prepared statements are not available.
旧的 MySQL API 有一个函数来转义 SQL 中使用的字符串,称为mysql_real_escape_string,但它从来没有用于防止 SQL 注入,自然不应该用于此目的。
新的 MySQLi APImysqli_real_escape_string为向后兼容性提供了一个替代功能,它与旧的存在相同的问题,因此除非准备好的语句不可用,否则不应使用。
The old mysql_* way:
旧的 mysql_* 方式:
$login = mysql_real_escape_string($_POST['login']);
$result = mysql_query("SELECT * FROM users WHERE user='$login'");
The prepared statement way:
准备好的语句方式:
$stmt = $mysqli->prepare('SELECT * FROM users WHERE user=?');
$stmt->bind_param('s', $_POST['login']);
$stmt->execute();
$result = $stmt->get_result();
Prepared statements in MySQLican look a little off-putting to beginners. If you are starting a new project then deciding to use the more powerful and simpler PDOAPI might be a good idea.
MySQLi 中的预处理语句对于初学者来说可能看起来有点令人反感。如果您正在开始一个新项目,那么决定使用更强大、更简单的PDOAPI 可能是个好主意。
3. Enhanced debugging capabilities
3. 增强的调试能力
Some old-school PHP developers are used to checking for SQL errors manually and displaying them directly in the browser as means of debugging. However, such practice turned out to be not only cumbersome, but also a security risk. Thankfully MySQLi has improved error reporting capabilities.
一些老派的 PHP 开发人员习惯于手动检查 SQL 错误并将它们直接显示在浏览器中作为调试的手段。但事实证明,这种做法不仅繁琐,而且存在安全隐患。幸运的是 MySQLi 改进了错误报告功能。
MySQLi is able to report any errors it encounters as PHP exceptions. PHP exceptions will bubble up in the script and if unhandled will terminate it instantly, which means that no statement after the erroneous one will ever be executed. The exception will trigger PHP Fatal error and will behave as any error triggered from PHP core obeying the display_errorsand log_errorssettings. To enable MySQLi exceptions use the line mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)and insert it right before you open the DB connection.
MySQLi 能够将它遇到的任何错误报告为 PHP 异常。PHP 异常会在脚本中冒泡,如果未处理将立即终止它,这意味着在错误之后不会执行任何语句。该异常将触发 PHP 致命错误,并将表现为任何由 PHP 核心触发的错误,并遵循display_errors和log_errors设置。要启用 MySQLi 异常mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT),请在打开数据库连接之前使用该行并插入它。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new \mysqli($host, $user, $password, $dbName);
$mysqli->set_charset('utf8mb4');
If you were used to writing code such as:
如果您习惯于编写代码,例如:
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
or
或者
$result = mysql_query('SELECT * WHERE 1=1') or die(mysql_error());
you no longer need to die()in your code.
你不再需要die()在你的代码中。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new \mysqli($host, $user, $password, $dbName);
$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT * FROM non_existent_table');
// The following line will never be executed due to the mysqli_sql_exception being thrown above
foreach ($result as $row) {
// ...
}
If for some reason you can't use exceptions, MySQLi has equivalent functions for error retrieval. You can use mysqli_connect_error()to check for connection errors and mysqli_error($mysqli)for any other errors. Pay attention to the mandatory argument in mysqli_error($mysqli)or alternatively stick to OOP style and use $mysqli->error.
如果由于某种原因您不能使用异常,MySQLi 具有等效的错误检索功能。您可以使用mysqli_connect_error()来检查连接错误和mysqli_error($mysqli)任何其他错误。注意强制参数mysqli_error($mysqli)或坚持 OOP 风格并使用$mysqli->error.
$result = $mysqli->query('SELECT * FROM non_existent_table') or trigger_error($mysqli->error, E_USER_ERROR);
See these posts for more explanation:
mysqli or die, does it have to die?
How to get MySQLi error information in different environments?
更多解释请看这些帖子:
mysqli or die,do it have to die?
不同环境下如何获取MySQLi错误信息?
4. Other changes
4. 其他变化
Unfortunately not every function from mysql_*has its counterpart in MySQLi only with an "i" added in the name and connection link as first parameter. Here is a list of some of them:
不幸的是,并不是每个函数mysql_*在 MySQLi 中都有对应的函数,只是在名称和连接链接中添加了一个“i”作为第一个参数。以下是其中一些的列表:
mysql_client_encoding()has been replaced bymysqli_character_set_name($mysqli)mysql_create_dbhas no counterpart. Use prepared statements ormysqli_queryinsteadmysql_drop_dbhas no counterpart. Use prepared statements ormysqli_queryinsteadmysql_db_name&mysql_list_dbssupport has been dropped in favour of SQL'sSHOW DATABASESmysql_list_tablessupport has been dropped in favour of SQL'sSHOW TABLES FROM dbnamemysql_list_fieldssupport has been dropped in favour of SQL'sSHOW COLUMNS FROM sometablemysql_db_query-> usemysqli_select_db()then the query or specify the DB name in the querymysql_fetch_field($result, 5)-> the second parameter (offset) is not present inmysqli_fetch_field. You can usemysqli_fetch_field_directkeeping in mind the different results returnedmysql_field_flags,mysql_field_len,mysql_field_name,mysql_field_table&mysql_field_type-> has been replaced withmysqli_fetch_field_directmysql_list_processeshas been removed. If you need thread ID usemysqli_thread_idmysql_pconnecthas been replaced withmysqli_connect()withp:host prefixmysql_result-> usemysqli_data_seek()in conjunction withmysqli_field_seek()andmysqli_fetch_field()mysql_tablenamesupport has been dropped in favour of SQL'sSHOW TABLESmysql_unbuffered_queryhas been removed. See this article for more information Buffered and Unbuffered queries
mysql_client_encoding()已被替换mysqli_character_set_name($mysqli)mysql_create_db没有对应物。使用准备好的语句或mysqli_query代替mysql_drop_db没有对应物。使用准备好的语句或mysqli_query代替mysql_db_name&mysql_list_dbs支持已被删除,转而支持 SQLSHOW DATABASESmysql_list_tables支持已被删除,转而支持 SQLSHOW TABLES FROM dbnamemysql_list_fields支持已被删除,转而支持 SQLSHOW COLUMNS FROM sometablemysql_db_query->mysqli_select_db()然后使用查询或在查询中指定数据库名称mysql_fetch_field($result, 5)-> 中不存在第二个参数(偏移量)mysqli_fetch_field。您可以使用mysqli_fetch_field_direct记住返回的不同结果mysql_field_flags,mysql_field_len,mysql_field_name,mysql_field_table&mysql_field_type-> 已替换为mysqli_fetch_field_directmysql_list_processes已被删除。如果您需要线程 ID,请使用mysqli_thread_idmysql_pconnect已被替换mysqli_connect()为p:主机前缀mysql_result->mysqli_data_seek()与mysqli_field_seek()和结合使用mysqli_fetch_field()mysql_tablename支持已被删除,转而支持 SQLSHOW TABLESmysql_unbuffered_query已被删除。有关缓冲和非缓冲查询的更多信息,请参阅本文
回答by Aiden Bell
I would tentatively recommend using PDOfor your SQL access.
我暂时建议您使用PDO进行 SQL 访问。
Then it is only a case of changing the driver and ensuring the SQL works on the new backend. In theory. Data migration is a different issue.
那么这只是更改驱动程序并确保 SQL 在新后端上工作的情况。理论上。数据迁移是一个不同的问题。
Abstract database access is great.
抽象数据库访问很棒。
回答by Svetoslav
In case of big projects, many files to change and also if the previous project version of PHP was 5.6 and the new one is 7.1, you can create a new file sql.php and include it in the header or somewhere you use it all the time and needs sql connection. For example:
在大型项目的情况下,要更改许多文件,并且如果 PHP 的先前项目版本是 5.6,而新的项目版本是 7.1,您可以创建一个新文件 sql.php 并将其包含在标题中或您使用它的地方时间和需要sql连接。例如:
//local
$sql_host = "localhost";
$sql_username = "root";
$sql_password = "";
$sql_database = "db";
$mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database );
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
// /* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
exit();
} else {
// printf("Current character set: %s\n", $mysqli->character_set_name());
}
if (!function_exists('mysql_real_escape_string')) {
function mysql_real_escape_string($string){
global $mysqli;
if($string){
// $mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database );
$newString = $mysqli->real_escape_string($string);
return $newString;
}
}
}
// $mysqli->close();
$conn = null;
if (!function_exists('mysql_query')) {
function mysql_query($query) {
global $mysqli;
// echo "DAAAAA";
if($query) {
$result = $mysqli->query($query);
return $result;
}
}
}
else {
$conn=mysql_connect($sql_host,$sql_username, $sql_password);
mysql_set_charset("utf8", $conn);
mysql_select_db($sql_database);
}
if (!function_exists('mysql_fetch_array')) {
function mysql_fetch_array($result){
if($result){
$row = $result->fetch_assoc();
return $row;
}
}
}
if (!function_exists('mysql_num_rows')) {
function mysql_num_rows($result){
if($result){
$row_cnt = $result->num_rows;;
return $row_cnt;
}
}
}
if (!function_exists('mysql_free_result')) {
function mysql_free_result($result){
if($result){
global $mysqli;
$result->free();
}
}
}
if (!function_exists('mysql_data_seek')) {
function mysql_data_seek($result, $offset){
if($result){
global $mysqli;
return $result->data_seek($offset);
}
}
}
if (!function_exists('mysql_close')) {
function mysql_close(){
global $mysqli;
return $mysqli->close();
}
}
if (!function_exists('mysql_insert_id')) {
function mysql_insert_id(){
global $mysqli;
$lastInsertId = $mysqli->insert_id;
return $lastInsertId;
}
}
if (!function_exists('mysql_error')) {
function mysql_error(){
global $mysqli;
$error = $mysqli->error;
return $error;
}
}
回答by Esty Shlomovitz
If you have a lot files to change in your projects you can create functions with the same names like mysql functions, and in the functions make the convert like this code:
如果您的项目中有很多文件要更改,您可以创建与 mysql 函数具有相同名称的函数,并在函数中进行如下代码的转换:
$sql_host = "your host";
$sql_username = "username";
$sql_password = "password";
$sql_database = "database";
$mysqli = new mysqli($sql_host , $sql_username , $sql_password , $sql_database );
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
function mysql_query($query){
$result = $mysqli->query($query);
return $result;
}
function mysql_fetch_array($result){
if($result){
$row = $result->fetch_assoc();
return $row;
}
}
function mysql_num_rows($result){
if($result){
$row_cnt = $result->num_rows;;
return $row_cnt;
}
}
回答by Meloman
Here is a complete tutorial how to make it quickly if you need to make worgking again a website after PHP upgrade. I used it after upgrading hosting for my customers from 5.4 (OMG!!!) to 7.x PHP version.
这是一个完整的教程,如果您需要在 PHP 升级后再次使 worgking 成为一个网站,如何快速制作它。我在为我的客户将托管从 5.4 (OMG!!!) 升级到 7.x PHP 版本后使用了它。
This is a workaround and it is better to rewrite all code using PDO or mysqli Class.
这是一种解决方法,最好使用 PDO 或 mysqli 类重写所有代码。
1. Connection definition
1. 连接定义
First of all, you need to put the connection to a new variable $linkor $con, or whatever you want.
首先,您需要将连接放置到一个新变量$link或$con,或任何您想要的。
Example
例子
Change the connection from :
更改连接:
@mysql_connect($host, $username, $password) or die("Error message...");
@mysql_select_db($db);
or
或者
@mysql_connect($host, $username, $password, $db) or die("Error message...");
to:
到:
$con = mysqli_connect($host, $username, $password, $db) or die("Error message...");
2. mysql_* modification
2.mysql_*修改
With Notepad++ I use "Find in files" (Ctrl + Shift + f) :
使用 Notepad++ 我使用“在文件中查找”(Ctrl + Shift + f):
in the following orderI choose "Replace in Files" :
我按以下顺序选择“在文件中替换”:
mysql_query( -> mysqli_query($con,
mysql_error() -> mysqli_error($con)
mysql_close() -> mysqli_close($con)
mysql_insert_id() -> mysqli_insert_id($con)
mysql_real_escape_string( -> mysqli_real_escape_string($con,
mysql_ -> mysqli_
mysql_query( -> mysqli_query($con,
mysql_error() -> mysqli_error($con)
mysql_close() -> mysqli_close($con)
mysql_insert_id() -> mysqli_insert_id($con)
mysql_real_escape_string( -> mysqli_real_escape_string($con,
mysql_ -> mysqli_
3. adjustments
3.调整
if you get errors it is maybe because your $con is not accessible from your functions.
如果出现错误,可能是因为您的函数无法访问您的 $con。
You need to add a global $con;in all your functions, for example :
您需要global $con;在所有函数中添加一个,例如:
function my_function(...) {
global $con;
...
}
In SQL class, you will put connection to $this->coninstead of $con. and replace it in each functions call (for example : mysqli_query($con, $query);)
在 SQL 类中,您将连接到$this->con而不是$con. 然后将其装在每个函数调用(例如:mysqli_query($con, $query);)
回答by Pichet Thantipiputpinyo
I have just created the function with the same names to convert and overwrite to the new one php7:
我刚刚创建了具有相同名称的函数来转换和覆盖新的 php7:
$host = "your host";
$un = "username";
$pw = "password";
$db = "database";
$MYSQLI_CONNECT = mysqli_connect($host, $un, $pw, $db);
function mysql_query($q) {
global $MYSQLI_CONNECT;
return mysqli_query($MYSQLI_CONNECT,$q);
}
function mysql_fetch_assoc($q) {
return mysqli_fetch_assoc($q);
}
function mysql_fetch_array($q){
return mysqli_fetch_array($q , MYSQLI_BOTH);
}
function mysql_num_rows($q){
return mysqli_num_rows($q);
}
function mysql_insert_id() {
global $MYSQLI_CONNECT;
return mysqli_insert_id($MYSQLI_CONNECT);
}
function mysql_real_escape_string($q) {
global $MYSQLI_CONNECT;
return mysqli_real_escape_string($MYSQLI_CONNECT,$q);
}
It works for me , I hope it will work for you all , if I mistaken , correct me.
它对我有用,我希望它对大家有用,如果我弄错了,请纠正我。


