Based on this code below I use for regular mysql, how could I convert it to use mysqli?
Is it as simple as changing **mysql _query($sql); to mysqli _query($sql); ?**
<?PHP
//in my header file that is included on every page I have this
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>");
mysql_select_db($DB['dbName']);
// end header connection part
// function from a functions file that I run a mysql query through in any page.
function executeQuery($sql) {
$result = mysql_query($sql);
if (mysql_error()) {
$error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
if ($_SESSION['auto_id'] == 1) {
$sql_formatted = highlight_string(stripslashes($sql), true);
$error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
}
die($error);
}
return $result;
}
// example query ran on anypage of the site using executeQuery function
$sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id'];
$result_member=executequery($sql);
if($line_member=mysql_fetch_array($result_member)){
extract($line_member);
} else {
header("location: index.php");
exit;
}
?>
I would tentatively recommend using PDO for your SQL access.
Then it is only a case of changing the driver and ensuring the SQL works on the new backend. In theory. Data migration is a different issue.
Abstract database access is great.
The easiest way i always handle this
Where $con = mysqli_connect($serverName,$dbusername,$dbpassword);
3 steps replacement in the following order
This works for me everytime
Short Version of converting mysql to mysqli
Here is a complete tutorial how to make it right and quickly. I used it after upgrading hosting for my customers from 5.4 (OMG!!!) to 7.x PHP version.
1. Connection definition
First of all, you need to put the connection to a new variable
$link
or$con
, or whatever you want.Example
Change the connection from :
or
to:
2. mysql_* modification
With Notepad++ I use "Find in files" (Ctrl + Shift + f) :
in the following order I choose "Replace in Files" :
mysql_query( -> mysqli_query($con,
mysql_error() -> mysqli_error($con)
mysql_close() -> mysqli_close($con)
mysql_ -> mysqli_
3. adjustments
if you get errors it is maybe because your $con is not accessible from your functions.
You need to add a
global $con;
in all your functions, for example :Hope it helps.
The first thing to do would probably be to replace every
mysql_*
function call with its equivalentmysqli_*
, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.To help with that, the The MySQLi Extension Function Summary is definitely something that will prove helpful.
For instance:
mysql_connect
will be replaced bymysqli_connect
mysql_error
will be replaced bymysqli_error
and/ormysqli_connect_error
, depending on the contextmysql_query
will be replaced bymysqli_query
Note that, for some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
For instance:
mysql_select_db
once connected, to indicate on which database you want to do your queriesmysqli_connect
.mysqli_select_db
function that you can use, if you prefer.Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)
In case of big projects, many files to change and also if the previous project version of PHP was 5.6 and the new one is 7.1, you can create a new file sql.php and include it in the header or somewhere you use it all the time and needs sql connection. For example: