Jump to content

mysqli-connect_error-script: from MySQL to MySQLi


malaga

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); ?

<?PHP

// 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>");
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;
}
?> 

 

 

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. 

mysql_query($query)
becomes

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.

 

 

Link to comment
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)!? 

Link to comment
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

 

<?php
$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);
}

$mysqli->close();


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. 

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines