Monday 24 September 2018

MySQLi functions to run a query and checking for errors occcured on last function call

After connecting database successfully, the next important task in an application is performing the query on the connected database. This article provides the details about how we can run a query to a MySQL database using mysqli extension. The article also provides the details about different useful functions which can be used to track the errors occurred with the query. The list and details of these functions are given below :


1. mysqli_query()

mysqli_query() function is used to perform a query on the database.

Result :  With non-DML statements such as SELECT, DESCRIBE, EXPLAIN etc., mysqli_query() will return a mysqli_result object on success of these queries. While with other queries such as INSERT, UPDATE, DELETE it will return TRUE on success and FALSE on failure.

Syntax :

1.1 Procedural Style : mysqli_query($con, $query, $result_mode) 


Description :
$con : MySQLi link identifier returned by mysqli_connect() or mysqli_init()(Required)
$query : Represents the query statement (SQL statement).(Required)
$result_mode : Represent the result mode. (Optional)

$result_mode can be one of the following :

a. MYSQLI_STORE_RESULT : This mode is used as default.
b. MYSQLI_USE_RESULT : This mode is used for retrieving large amount of data.

Example :
  1. <?php
  2. $con = @mysqli_connect($host_name, $username, $password, $database);
  3. /* check connection errors */
  4. if (!$con) {
  5. die('Connect Error: ' . mysqli_connect_errno());
  6. }
  7. $query = "CREATE DATABASE demo";
  8. //execute query
  9. if (mysqli_query($con, $query)) {
  10. echo "Database created successfully";
  11. } else {
  12. echo "Error in creating database";
  13. }
  14. mysqli_close($con);
  15. ?>
Output : Database created successfully

1.2 Object Oriented Style : $con->query($query, $result_mode)

$query and $result_mode parameters are same as defined above.

Example :
  1. <?php
  2. // Create connection
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. if ($con->connect_errno) {
  5. die('Connect Error: ' . $con->connect_error);
  6. }
  7. $query = "CREATE DATABASE demo_db";
  8. //ececute query
  9. if ($con->query($query)) {
  10. echo "Database created successfully";
  11. }
  12. // close connection
  13. $con->close();
  14. ?>
Output : Database created successfully

2. mysqli_errorno()

This function returns the error code for the last function call with respect to mysqli link whether it succeed or fail. If no error occurred, it returns 0.

Syntax :

2.1 Procedural Style : mysqli_errno($con)

$con : Specifies the MySQLi link identifier. (Required)

Example :
  1. <?php
  2. //create connection
  3. $con = @mysqli_connect($host_name, $username, $password, $database);
  4. // check connection errors
  5. if (!$con) {
  6. die('Connect Error: ' . mysqli_connect_errno());
  7. }
  8. $query = "CREATE DATABASE demo_db";
  9. //execute query and check for error
  10. if (!mysqli_query($con, $query)) {
  11. echo "Error Code :". mysqli_errno($con);
  12. }
  13. //close connection
  14. mysqli_close($con);
  15. ?>
Output : Error Code :1007

2.2 Object Oriented Style : $con->errno

Example :
  1. <?php
  2. //create connection
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. //check connection error
  5. if ($con->connect_errno) {
  6. die('Connect Error: ' . $con->connect_error);
  7. }
  8. $query = "CREATE DATABASE demo_db";
  9. //ececute query and check error
  10. if (!$con->query($query)) {
  11. echo "Error Code : ". $con->errno;
  12. }
  13. //close connection
  14. $con->close();
  15. ?>
Output : Error Code :1007

3. mysqli_error()

The function mysqli_error() is similar to mysqli_errno(). The only difference is, it returns the error message instead of error code for the most recent function call.

Result : Returns a string if any error occurred on recent function call and if no error occurred, it returns an empty string ("").

Syntax :

3.1 Procedural Style : mysqli_error($con)

$con : Specifies the MySQLi link identifier. (Required)

Example :
  1. <?php
  2. //create connection
  3. $con = @mysqli_connect($host_name, $username, $password, $database);
  4. // check connection errors
  5. if (!$con) {
  6. die('Connect Error: ' . mysqli_connect_errno());
  7. }
  8. $query = "CREATE DATABASE demo_db";
  9. //execute query and check errors
  10. if (!mysqli_query($con, $query)) {
  11. echo "Error : ". mysqli_error($con);
  12. }
  13. // close connection
  14. mysqli_close($con);
  15. ?>
Output : Error : Can't create database 'demo_db'; database exists

3.2 Object Oriented Style : $con->mysqli->error


Example :
  1. <?php
  2. // Create connection
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. //check connection errors
  5. if ($con->connect_errno) {
  6. die('Connect Error: ' . $con->connect_error);
  7. }
  8. $query = "CREATE DATABASE demo_db";
  9. //execute query and check error
  10. if (!$con->query($query)) {
  11. echo "Error : ". $con->error;
  12. }
  13. // close connection
  14. $con->close();
  15. ?>
Output : Error : Can't create database 'demo_db'; database exists

4. mysqli_error_list()

Like above two functions, this function also returns a list of errors in the last function call.

Result : Returns a list of errors as an associative array containing errno (error code), error (error statement) and sqlstate.

Syntax :

4.1 Procedural Style : mysqli_error_list ($con)

$con : Specifies the MySQLi link identifier.(Required)

Example :
  1. <?php
  2. //create connection
  3. $con = @mysqli_connect($host_name, $username, $password, $database);
  4. // check connection errors
  5. if (!$con) {
  6. die('Connect Error: ' . mysqli_connect_errno());
  7. }
  8. $query = "CREATE DATABASE demo_db";
  9. //execute query and check error
  10. if (!mysqli_query($con, $query)) {
  11. echo "<pre>";
  12. print_r(mysqli_error_list($con));
  13. echo "</pre>";
  14. }
  15. // close connection
  16. mysqli_close($con);
  17. ?>

Output :
Array
(
    [0] => Array
        (
            [errno] => 1007
            [sqlstate] => HY000
            [error] => Can't create database 'demo_db'; database exists
        )
)

4.2 Object Oriented Style : $con->error_list


Example :
  1. <?php
  2. // Create connection
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. if ($con->connect_errno) {
  5. die('Connect Error: ' . $con->connect_error);
  6. }
  7. $query = "CREATE DATABASE demo_db";
  8. //execute query and check error
  9. if (!$con->query($query)) {
  10. echo "<pre>";
  11. print_r($con->error_list);
  12. echo "</pre>";
  13. }
  14. // close connection
  15. $con->close();
  16. ?>

Output :
Array
(
    [0] => Array
        (
            [errno] => 1007
            [sqlstate] => HY000
            [error] => Can't create database 'demo_db'; database exists
        )
)

0 comments:

Post a Comment