我正在将很多文件从 mysql 更改为 mysqli。我正在使用它来做到这一点:https ://wikis.oracle.com/display/mysql/Converting+to+MySQLi
我的连接文件(为了简单起见,我删除了我认为不必要的代码,例如错误处理)会产生我不确定如何修复的错误。原始代码:
$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"));
上面的代码只是我连接到每个文件中使用的数据库文件
include_once("includes/connnect.php");
连接到数据库和表的前面的代码必须与下面的代码一起使用,因为我有很多类似下面的查询,并且更改 connect.php 文件比更改许多文件更容易:
$con1 = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM table1 WHERE id='$id'");
编辑 - 更新为包含我在复制和粘贴时错过的分号。
编辑 2 - 包括来自转换工具的错误/警告消息
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);
编辑 - 下面的工作答案。转换工具适用于我所有其他文件。只需在我的连接文件中更改它连接到数据库的方式
$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 是端口。要查找端口,请使用以下命令:
$port = ini_get("mysql.default_port");
echo "the port ". $port;