Python Forum
from MySQL to MySQLi - the script for errors in the db-connection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from MySQL to MySQLi - the script for errors in the db-connection
#1
dear Python-experts.

first of all: note i have plenty of issues - a whole bunch of issues on a Server - i need to digg deeper into all things regarding creating a healthy db-connection. Therefore i need to learn all about the script - mysql_connect_error... see below.

i have gathered some information - in order to think out it - and to share the ideas ... so i hopefully will learn and solve my issues. It is my belive that i need to learn about the techniques that help me to get more infos about the server.

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.



what do you think bout this..



love to hear from you
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply
#2
(Jun-19-2019, 08:16 PM)apollo Wrote: what do you think bout this..
I think you should ask people who know PHP, since your question is about the various mysql interfaces php is providing to you.
Reply
#3
HELLO and good eveing dear nilamo,

many many thanks for the quick reply - i am glad to hear from you.


(Jun-19-2019, 08:25 PM)nilamo Wrote:
(Jun-19-2019, 08:16 PM)apollo Wrote: what do you think bout this..
I think you should ask people who know PHP, since your question is about the various mysql interfaces php is providing to you.

i will do so - it is a good idea.

many many thanks
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply
#4
I wish i could answer your PHP and MySQL questions. But unfortunately, those are the things i am least knowledgeable in.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020