Monday, 24 September 2018

How to check connection errors and change default database in MySQLi

My previous articles titled Connecting MySQL database with MySQLi Object Oriented style and How to connect MySQL database using the MySQLi procedural function demonstrate about different ways to connect to a MySQL database with PHP application. Those articles define the way through which an application connects to the database, but MySQLi also provides some other very useful function which can be used to check connection errors or to change the default database for performing queries. This article defines the list of these functions :

1. mysqli_connect_errorno() :

This function returns the error code occurred on the last connection call made by using mysqli_connect() function.
Result : The function returns an error code (a number) on failure, otherwise it returns 0 on successful connection.
Syntax :
1.1 Procedural Style : mysqli_connect_errno()
Example :
  1. <?php
  2. $con = mysqli_connect('hostname', 'username', 'password', 'dbname');
  3. if (!$con) {
  4. die('Connection Error: ' . mysqli_connect_errno());
  5. }
  6. ?>
Output : Connect Error: 1045

1.2 Object Oriented Style : $con->$connect_errno
Example :
  1. <?php
  2. $con = @new mysqli('hostname', 'username', 'password', 'dbname');
  3. if ($con->connect_errno) {
  4. die('Connection Error: ' . $con->connect_errno);
  5. }
  6. ?>
Output : Connect Error: 1045

 

2. mysqli_connect_error() :

This function is similar to mysqli_connect_errorno(), the only difference is, it returns the error description in the form of a string instead of error code for the last connection call made by mysqli_connect().
Result : This function returns error description, otherwise it returns NULL on successful connection.
Syntax :
2.1 Procedural Style : mysqli_connect_error ()
Example :
  1. <?php
  2. $con = @mysqli_connect('hostname', 'username', 'password', 'dbname');
  3. if (!$con) {
  4. die('Connection Error: ' . mysqli_connect_error());
  5. }
  6. ?>
Output : Connect Error: Access denied for user 'root'@'localhost' (using password: YES)

2.2 Object Oriented Style : $con->connect_error
Example :
  1. <?php
  2. $con = @new mysqli('hostname', 'username', 'password', 'dbname');
  3. if ($con->connect_error) {
  4. die('Connection Error: ' . $con->connect_error);
  5. }
  6. ?>
Output : Connect Error: Access denied for user 'root'@'localhost' (using password: YES)

Suggestion : It's better to use mysqli_connect_error() with PHP versions earlier to 5.2.9 and 5.3.0 to avoid compatibility issues.
Note : For mysqli_connect_errorno() and mysqli_connect_error(), I have used wrong password to check errors.

 

3. mysqli_select_db() :

This function is used to change the default database. It can also used to set default database, if it's not set using mysqli_connect().
 
Result :  This function returns True on success and False on failure.

Syntax :
3.1 Procedural Style : mysqli_select_db ($con, $dbname )
Description :
$con : MySQLi link identifier.(Required)
$dbname : Specifies database name.(Required)

Example :
  1. <?php
  2. $con = mysqli_connect("hostname", "username", "password", "dbname");
  3. /* check connection */
  4. if (!$con) {
  5. die('Connection Error: ' . mysqli_connect_error());
  6. }
  7. /* print default database name */
  8. if ($result1 = mysqli_query($con, "SELECT DATABASE()")) {
  9. $row = mysqli_fetch_row($result1);
  10. echo "Current Database : ".$row[0];
  11. echo "<br>";
  12. }
  13. /* select another database */
  14. mysqli_select_db($con, "demo");
  15. if ($result2 = mysqli_query($con, "SELECT DATABASE()")) {
  16. $row = mysqli_fetch_row($result2);
  17. echo "Current Database : ".$row[0];
  18. }
  19. ?>
Output : Current Database : test
Current Database : demo

3.2 Object Oriented Style : $con->select_db($dbname)
$dbname: Specifies the name of database.

Example :
  1. <?php
  2. $con = new mysqli("hostname", "username", "password", "dbname");
  3. /* check for connection */
  4. if ($con->connect_errno) {
  5. die('Connection Error: ' . $con->connect_errno);
  6. }
  7. /* print default database name */
  8. if ($result1 = $con->query("SELECT DATABASE()")) {
  9. $row = $result1->fetch_row();
  10. echo "Current Database :".$row[0];
  11. echo "<br>";
  12. }
  13. /* change database */
  14. $con->select_db("demo");
  15. if ($result2 = $con->query("SELECT DATABASE()")) {
  16. $row = $result2->fetch_row();
  17. echo "Current Database :".$row[0];
  18. }
  19. ?>
Output : Current Database : test
Current Database : demo.

 

4. mysqli_close() :

This function is used to close a previously opened database connection.

Return : It returns TRUE on success and FALSE on failure.

Syntax :
4.1 Procedural Style : mysqli_close($con)
$con : Specifies MySQLi link.(Required)

Example :
  1. <?php
  2. $con = mysqli_connect("hostname", "username", "password", "dbname");
  3. /*
  4. * PHP code
  5. */
  6. //---to close connection---
  7. mysqli_close($con);
  8. ?>

4.2 Object Oriented Style : $con->close();
Example :
  1. <?php
  2. //--create connection---
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. /*
  5. * PHP code
  6. */
  7. //---close connection---
  8. $con->close();
  9. ?>

0 comments:

Post a Comment