tutorial mysqli-connect_error-script: from MySQL to MySQLi

Recommended Posts

Hello dear experts at Armbian 


well the mysql_connect_error-script is a great help: It returns a string description of the last connect error ( see more infos here: https://www.php.net/manual/en/mysqli.connect-error.php ).  The mysqli_connect_error() function returns the error description from the last connection error, if there is  any error-note. 

the return value are the following ones:

a. A string that describes the error.
b. an empty string if no error occurred.

at least this goes for the Version: PHP 5, PHP 7

well - if we run the code below we can get the info bout the option to connect to the db.  What if we run this as a mysql-test-script, and what if we will want to convert it to use mysqli? Can this be done by changing mysql _query($sql); to mysqli _query($sql); ?


// the test-script that we are running.
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>Howdy - be aware; There a thing happenede - 
An Internal Error has Occured. Please report following error to the webmaster shot him a mail now.<br><br>".mysql_error()."'</center>");
// 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();
    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'];
} else {
    header("location: index.php");



If we do replace mysql_* with mysqli_* then we will have to bear in mind that a whole load of mysqli_* functions need  the database link to be passed.

E.g.: the following ones. 


mysqli_query($link, $query)
I.e., lots of checking required.

on the other hand side: 

is it suffice if we replace every mysql_* function call with its equivalent mysqli_*, when we will use the procedural API 
 (note: there is some code based on the MySQL API, which is a procedural one - at least afaik), To help with that, the The MySQLi Extension Function Summary-manual is definitely something that will prove helpful. We can do the following: 


we have the following options to do that: 


- mysql_connect will be replaced by mysqli_connect
- mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
- mysql_query will be replaced by mysqli_query ,,,, and so on and so forth. 


Note: For some functions, we may need to check the parameters very very carefully: Maybe there are 
some differences here and there -- but not that many differences. Belive me. Both mysql and mysqli-codes are based on the same library ( the great and powerful libmysql ; at least for PHP-version <= 5.2)

Usage - for instance:
with mysql, we have to use the mysql_select_db once connected, to indicate on which database we want to do our queries mysqli, on the other side, allows us to specify that database name as the fourth parameter to mysqli_connect.



Share this post

Link to post
Share on other sites

some words regarding the installation:  


I have Linux Server. Apache 2.4.10, PHP Version 5.6.39 and mysqlnd 5.0.11-dev - 20120503 -
installed. There are several wordpress website running on server. I have  checked this all one week ago, In the phpinfo(); 

what do you suggest - should i ask my serveradmin that he will update & upgrade the whole system - in order to have a modern system... And then i run the mysqli_error_(function)!? 

Share this post

Link to post
Share on other sites

hello dear all 


i am trying to make sure that i have set up the MySQL use to be able to access the database via localhost? - this is pretty important for me - i need to have more insights! Aferward i have to try using the following code to connect to the DB


$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

 * This is the "official" OO way to do it,
 * BUT wait - the $connect_error was broken until PHP 5.2.9 and 5.3.0. - so i have to make sure if i am the one that must be careful here
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);


well i have to do some more test. 

dear experts here at the great Armbian-Forums - i keep you posted - and i come back and report all the findings. 



Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.