hello dear experts of XAMPP and bona sera dear Apachefriends,
good day Nobbie and all the user of Xampp... Wampp and Lampp
some more notes on the topic - how to establish a db connection and what to do when it does not work
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.
but wait - regarding the comparison of mysql and mysql i and PDO - here some ideas
1. The now extinct mysql library and the mysqli library are two completely different animals.
2. so we c an just forget about mysqli and use PDO.
in a picture
+---------+-------------+
| mysql | dodo |
| mysqli | donkey |
| PDO | racehorse |
+---------+-------------+
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); ?
- Code: Select all
<?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.
well Wahat do you think bout this...
for Wordpress-development :: super toolkits :: . github.com/miziomon/awesome-wordpress
:: Awesome WordPress: A curated list of amazingly awesome WordPress resources, themes, plugins and shiny things.