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;