Changing from mysql to mysqli code error

2019-01-27 05:14发布

问题:

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);

New code:

$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");

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.

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!

Where line 14 is

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

and line 16 is

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 is the port. To find the port, use the below:

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

回答1:

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

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.

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().

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 :-).

Just to note, I would not say:

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.

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.



回答2:

You forgot a semicolon:

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


标签: php mysql mysqli