MySQLi Function in PHP: Part 3

In this article I describe the PHP MySQLi functions mysqli_debug, mysqli_dump_debug_info, mysqli_errno, mysqli__error_list and mysqli_error.

Introduction

In this article I describe the PHP MySQLi  functions mysqli_debug, mysqli_dump_debug_info, mysqli_errno, mysqli__error_list and mysqli-error. To learn some other MySQLi functions, go to:

  1. MySQLi Function in PHP: Part 1
  2. MySQLi Function in PHP: Part 2

PHP mysqli_debug() Function

The PHP MySQLi  mysqli_debug function performs debugging operations and this function returns true.

Syntax

mysql_debug(message)

Parameter of the mysqli_debug function

The parameter of the function is: 

Parameter Description
message It specifies a string that represents the debugging operation to perform.

Example


The following example creates a trace file in "/tmp/client.trace" on the local (client) machine.

<?
php
mysqli_debug
("d:t:o,/temp/client.trace");
?>

PHP mysqli_dump_debug_info() Function

The PHP MySQLi "mysqli_debug" function dumps debugging information into the log and this function returns true on success or false on failure.

Syntax

mysql_dump_debug_info(link)

Parameter of the mysqli_dump_debug_info function

The parameter of the function is: 

Parameter Description
link It specifies a link identifier returned by mysqli_connect() or mysqli_init() function.

Example

An example of the function is:

<?php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_error($con))
{
echo "
Failed  to  connect  to  MySQL:  " . mysqli_connect_error();
}
mysqli_dump_debug_info($con);

mysqli_close($con);

?>

PHP mysqli_errno() Function

The PHP MySQLi "mysqli_errno" function returns the last error code for the most recent function call or in other words this function returns an error value of the last call, if it failed otherwise it returns zero, meaning that no error occured.

Syntax

mysql_errno(connection)

Parameter of the mysqli_errno function

The parameter of the function is: 

Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:

<?php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_error($con))
{
echo "
Failed  to  connect  to  MySQL:  " . mysqli_connect_error();
}
if (!mysqli_query($con,"
INSERT INTO emp (id,name,salary) VALUES (101,'Rahul',667897)"))    //error free
{
echo("
Errorcode: " . mysqli_errno($con));
}

/*........For Fetch data of Emp Table<br>

  .......*/

print "<h2>MySQL: Data of Emp table</h2>";

$result =mysqli_query($con,"SELECT * FROM emp");

echo "<table border='1'>

<tr>

<th>Id</th>

<th>Name</th>

<th>Salary</th>

</tr>";

while($rowval = mysqli_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $rowval['id'] . "</td>";

  echo "<td>" . $rowval['name'] . "</td>";

  echo "<td>" . $rowval['salary'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

/*........For Fetch data of Emp Table<br>

  .......*/

 

print "<h2>Write a query with error:</h2>"; 

if (!mysqli_query($con,"INSERT INTO emp (id,name,salary) VALUES (109,'Rahul',667897,'admin')")) 

{

//I try to insert "admin" as a role in "emp" table, and that column is not there.

echo("Errorcode: " . mysqli_errno($con));

}

mysqli_close($con);

?>


Output


mysqli-errno-function-in-php.jpg


PHP mysqli_error_list() Function

The PHP MySQLi "mysqli_errorlist" function returns a list of errors from the last command executed or in other words this function returns a list of errors, each as an associative array containing the errno, error, and sqlstate.

Syntax

mysql_error_list(connection)

Parameter of the mysqli_error_list function

The parameter of the function is: 

Parameter Description
connection It specifies MySQL connection to use.

Example

An example of the function is:

<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_error($con))
{
echo "
Failed  to  connect  to  MySQL:  " . mysqli_connect_error();
}
if (!mysqli_query($con,"
INSERT INTO emp  (id,name,salary) VALUES (107,'Sumit',654675)")) //error free
{
print_r(mysqli_error_list($con));
}
 /*........For Fetch data of Emp Table<br>
  .......*/

print "<h2>MySQL: Data of Emp table</h2>";

$result =mysqli_query($con,"SELECT * FROM emp");

echo "<table border='1'>

<tr>

<th>Id</th>

<th>Name</th>

<th>Salary</th>

</tr>";

while($rowval = mysqli_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $rowval['id'] . "</td>";

  echo "<td>" . $rowval['name'] . "</td>";

  echo "<td>" . $rowval['salary'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

/*........For Fetch data of Emp Table<br>

  .......*/

 

print "<h2>Write a query with error:</h2>"; 

if (!mysqli_query($con,"INSERT INTO emp  (id,name,salary) VALUES (107,'Sumit',654675,'admin')"))

{

print_r(mysqli_error_list($con));

}

mysqli_close($con);

?>

Output

mysqli-error-list-function-in-php.jpg 

PHP mysqli_error() Function

The PHP MySQLi "mysqli_error" function returns a string description of the last error or in other words this function returns a string that describes the error or returns an empty string if no error occured.

Syntax

mysql_error(connection)

Parameter of the mysqli_error function

The parameter of the function is: 

Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:

<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($con))
{
echo "
Failed  to  connect  to  MySQL:  " . mysqli_connect_error();
}
print "<
h2>Write a query with error:</h2>"; 
if (!mysqli_query($con,"INSERT INTO emp  (id,name,salary) VALUES (107,'Sumit',654675,'admin')"))
{
echo("Errorcode: " . mysqli_error($con));
}
mysqli_close($con);

?>

Output

mysqli-error-function-in-php.jpg