从mysql更改为mysqli代码错误 [英] Changing from mysql to mysqli code error

查看:39
本文介绍了从mysql更改为mysqli代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将许多文件从mysql切换到mysqli.我正在使用它来做: https://wikis.oracle.com/display/mysql/Converting + to + MySQLi

I'm changing over a lot of files from mysql to mysqli. I'm using this to do it: https://wikis.oracle.com/display/mysql/Converting+to+MySQLi

我的连接文件(为了简单起见,我已经删除了不必要的代码,例如错误处理)会产生无法确定如何修复的错误. 原始代码:

My connect file (I've taken out what I think is unnecessary code such as error handling for simplicity) creates errors that I'm not sure how to fix. Orignial code:

$conn=mysql_connect ("$localhost", "$dbusername", "$dbpass");

mysql_select_db("$db", $conn);

新代码:

$conn=($GLOBALS["___mysqli_ston"] = mysqli_connect("$localhost",  "$dbusername",  "$dbpass")); 

((bool)mysqli_query( $conn, "USE $db"));

上面的代码只是我连接到数据库文件的方式,该文件通过以下方式在每个文件中使用

The above code is just my connect to database file which is used in every file via

include_once("includes/connnect.php");

连接到数据库和表的先前代码必须与下面的代码一起使用,因为我有很多类似下面的查询,并且更改connect.php文件比许多文件更容易:

The previous code which connects to the database and table must work with the below code because I've got many queries like the below and it's easier to change the connect.php file than many files:

$con1 = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM table1 WHERE id='$id'"); 

编辑-更新为包括我在复制和粘贴时错过的分号.

Edit - updated to include a semi colon that I missed while copying and pasting.

编辑2-包含来自转换工具的错误/警告消息

Edit 2 - Including error/warning messages from conversion tool

14  [Line 14] Please check your code for parse errors, we failed to parse " ". Conversion will be incomplete!".
14  [Line 14] Cannot analyze server parameter to extract host, socket and port! Conversion cannot be performed automatically. You must manually check the result of the conversion.
16  [Line 16] mysql_select_db(string database_name [...]) is emulated using mysqli_query() and USE database_name. This is a possible SQL injection security bug as no tests are performed what value database_name has. Check your script!

第14行是

$conn=mysql_connect ("$localhost", "$dbusername", "$dbpass"); 

第16行是

mysql_select_db("$db", $conn);

编辑-工作答案.转换工具非常适合我所有其他文件.只需在我的连接文件中更改它连接数据库的方式

EDIT - Working answer below. Conversion tool worked perfectly for all my other files. Just had to change the way it connected to the database in my connect file

$conn=($GLOBALS["___mysqli_ston"] = mysqli_connect("$localhost",  "$dbusername",  "$dbpass", "$db", "3306")) or die ('Cannot connect to the database because: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));

3306是端口.要找到端口,请使用以下命令:

3306 is the port. To find the port, use the below:

$port = ini_get("mysql.default_port");
echo "the port ". $port;

推荐答案

14 [第14行]请检查您的代码是否存在解析错误,因为我们无法解析".转换将不完整!".

14 [Line 14] Please check your code for parse errors, we failed to parse " ". Conversion will be incomplete!".

此错误是由mysql_connect()调用中(之前的空格引起的.用$conn=mysql_connect("$localhost", "$dbusername", "$dbpass");替换它会删除MySQLConverterTool的警告输出.

This error is caused by the space before the ( in your mysql_connect() call. Replacing it with $conn=mysql_connect("$localhost", "$dbusername", "$dbpass"); removes this warning output by MySQLConverterTool.

剩下的两个错误是您应该自己亲自看一下 mysql_connect()之间的区别时应该处理的事情. mysqli_connect() . mysql_connect()的第一个参数$server的格式可以类似于hostname:port,而使用mysqli_connect()时,您只需将hostname传递给它的第一个参数,然后将port作为可选的第五个参数传递.另外,mysqli会让您在mysqli_connect()调用中指定数据库,而不要具有类似于mysql_select_db()的单独功能.

The remaining two errors are things that you should deal with by actually looking, yourself, at the difference between mysql_connect() and mysqli_connect(). mysql_connect()’s first argument, $server, can be formatted like hostname:port whereas with mysqli_connect() you would pass only hostname to its first argument and pass port as an optional fifth parameter. Also, mysqli would have you specify the database in the mysqli_connect() call instead of having a separate function analogous to mysql_select_db().

我建议,如果需要,您可以使用转换器工具将所有源代码从mysql转换为mysqli ,这些行中带有警告.只有您知道"$localhost"是哪种格式的:如果它可能包含端口信息,则必须将端口信息分开.您可能应该将数据库设置为在mysqli_connect()中使用,而不是使用转换器的自动USE $db填充程序.这正是转换器试图告诉您的:-).

I suggest that, if you need, you use the converter tool to convert all of your sourcecode from mysql to mysqli except for these lines with the warnings in them. Only you know what format "$localhost" comes in: if it might contain port information, you must separate the port information out. You should probably set the database to use in mysqli_connect() instead of using the converter’s automatic USE $db shim. This is exactly what the converter is trying to tell you :-).

请注意,我不会说:

我的连接文件(为了简单起见,我已经删除了不必要的代码,例如错误处理)会产生无法确定如何修复的错误.

My connect file (I've taken out what I think is unnecessary code such as error handling for simplicity) creates errors that I'm not sure how to fix.

以上内容表明,转换器生成的PHP代码本身在运行时引发PHP警告和错误(不是转换器在抱怨您的原始代码或告知您实际上需要进行一些手动转换)以上讨论).这就是为什么我们一直在寻找错误,例如您曾经纠正的一次丢失的分号.

The above suggests that the PHP code generated by the converter is, itself, throwing PHP warnings and errors at runtime (not that the converter is complaining about your original code or informing you that you need to actually do some manual conversion as I discussed above). That is why we were looking for errors like the once-missing semicolon which you corrected.

这篇关于从mysql更改为mysqli代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆