Going from mysql to mysqli error

I am changing a lot of files from mysql to mysqli. I use this for this: https://wikis.oracle.com/display/mysql/Converting+to+MySQLi

My connections file (I just chose unnecessary code, for example for error handling) creates errors that I don't know how to fix. Indicative 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 database file connection which is used in each file via

include_once("includes/connnect.php");

      

The previous code that connects to the database and table should work with the below code because I have many requests as below and it is easier to modify the connect.php file than many files:

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

      

Edit - Updated to include a half-line I missed during copy and paste.

Edit 2 - Include error / warning messages from the 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!

      

If line 14

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

      

and line 16 is

mysql_select_db("$db", $conn);

      

EDIT - Working answer below. The conversion tool worked great for all my other files. Just had to change the way I connect to the database in my connection 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 a port. To find the port use below:

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

      

0


source to share


3 answers


14 [Line 14] Please check your code for parsing errors, we were unable to parse "The conversion will be incomplete!".

This error is caused by a space before (

in your call mysql_connect()

. Replacing it with $conn=mysql_connect("$localhost", "$dbusername", "$dbpass");

removes this warning output using MySQLConverterTool.

The other two errors are what you have to deal with while actually looking at the difference between mysql_connect()

and mysqli_connect()

. mysql_connect()

s the first argument $server

can be formatted as hostname:port

, whereas with s mysqli_connect()

you only pass hostname

in your first argument and pass port

as an optional fifth parameter. Also, to mysqli you would specify the database in the call mysqli_connect()

instead of a separate function like mysql_select_db()

.

I suggest that if you need to, you use a converter tool to convert all source code from mysql to mysqli, except for these warning lines in them. Only you know what the format "$localhost"

is in: if it can contain port information, you must separate the port information. You should probably set the database to use in mysqli_connect()

instead of automatically converting USE $db

converters. This is exactly what the converter is trying to tell you :-).



Just to point out, I wouldn't say:

My connections file (I realized that I consider unnecessary code, such as error handling for simplicity) generates errors that I'm not sure how to fix.

From the above, it follows that the PHP code generated by the converter itself throws PHP warnings and errors at runtime (not that the converter complains about your source code or informs you that you really need to do a manual conversion, since I discussed above). This is why we looked for bugs like the invalid semicolon that you fixed.

+2


source


Did you forget the semicolon:



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

      

0


source


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

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

      

I tried the code, my result is a port but not a number?

-1


source







All Articles