Converting MySQL to MySQLi Assistance please


I have some PHP & MySQL commands that I want to convert to MySQLi.

I was using a technique with MySQL that allowed the scripts to have multiple databases and tables open simultaneously. Here is an example:

$sql_invoice_details = "SELECT * FROM sales.invoice_details WHERE invoice_id = '" . $invoice_id . "' AND internal_num = " . $internal_num . " ORDER BY display_order";
if(!$result_invoice_details = mysql_query($sql_invoice_details))
				die(mysql_error().'<BR><BR>SQL_INVOICE_DETAILS: '.$sql_invoice_details);

And in the same script:
$sql_30day = "SELECT * FROM sales.sc_sales WHERE internal_num = " . $internal_num . " AND prod_id LIKE 'BUSAPA003%'";
						if(!$result_30day = mysql_query($sql_30day))
								die(mysql_error().'<BR><BR>SQL_30DAY: '.$sql_30day);

This way I do not use MySQL_select_db()

What is the best and most secure way the convert to mysqli ?

Thank you in advance

The way you do this querying of multiple tables has nothing to do with PHP. You leave it up to MySQL to supply the tables from the correct database. You should be able to do the same with MySQLi.
While you’re in the process of migrating to MySQLi (which I asume), you might also consider moving to PDO. The differences are not that big. Try searching a bit to find the exact differences.

1 Like


Thanks for replying. However with some research I have found what I required.

For information for other people:
Point 1
The database is optional and if omitted in the connecting statement all you have to do is add the database name to the table name like this:


Point 2
You can setup multiple instances of mysqli by using different link names like this:

$db1 = mysqli(‘localhost’, ‘user’, ‘pass’)

$db2 = mysqli(‘localhost’, ‘user’, ‘pass’)

and then open up more than 1 database simultaneously.

Hope this helps anyone else

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.