MySQLi Function in PHP: Part 2

Introduction

In this article I describe the PHP MySQLi  functions mysqli_commit, mysqli_connect_errno, mysqli_connect, mysqli_connect_error and mysqli_data_seek. To learn some other MySQLi functions, go to:

  1. MySQLi Function in PHP: Part 1

PHP mysqli_commit() Function

The PHP MySQLi "mysqli_commit" function commits the current transactions and this function returns true on success or false on failure.

Syntax

mysql_commit(connection)

Parameter in mysqli_commit 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();
   } 

 mysqli_autocommit($con,FALSE);  

 

 mysqli_query($con,"INSERT  INTO  emp  VALUES (106,  'Atul', 656456)");

 echo "Affected  rows:  " . mysqli_affected_rows($con);

  /*........For Fetch data of Emp Table

   .......*/

 //mysql_select_db("mysql", $con);

 

 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

   .......*/

   mysqli_commit($con); // commits the current transaction
 

 mysqli_close($con);

 ?>


Output

mysqli-commit-funnction-in-php.gif


PHP mysqli_connect_error_no() Function

The PHP MySQLi "mysqli_connect_error_no" function returns the error code from the last connected call, if an error occurs.

Syntax

mysqli_connect_errno()

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_errno();
   }
    mysqli_close($con);

 ?>

No problem; the code above will be run, so now I am changing the code above. Suppose that the MySQL database has no password, and I am trying to pass a password in the mysqli_connect(), so what will happen? Let's see:

 

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

  ?>

Output

mysqli-connect-errno-function-in-php.gif

PHP mysqli_connect() Function

The PHP MySQLi "mysqli_connect" function opens a new connection to the MySQL server and this function returns a MySQL link identifier on success or false on failure.

Syntax

mysqli_connect(hostname,username,password,dbname,port,socket)

Parameters in the mysqli_connect function

The parameters of the function are: 

Parameter Description
host It specifies a host name or an IP address.
username It specifies the MySQL username.
password It specifies MySQL password.
dbname It specifies the default database to be used when performing queries.
port It specifies the port number to attempt to connect to MySQL server.
socket It specifies the socket or named pipe to be used.

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();

   }

else

   {

   echo "No error!, <b>connection has been established.</b>";

   }

mysqli_close($con);

 ?>

Output

sqli-connect-function-in-php.gif

PHP mysqli_connect_error() Function

The PHP MySQLi mysqli_connect_error  function returns a string description of the last connect error or say this function returns a string that describes the error on success and returns null if no error occurred.

Syntax

mysqli_connect_error()

Example

An example of the function is:

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

  ?>

Output

mysqli-connect-error-function-in-php.gif 

PHP mysqli_data_seek() Function

The PHP MySQLi "mysqli_data_seek" function adjusts the result pointer to an arbitrary row in the result and this function returns true on success or false on failure.

Syntax

mysql_data_seek(result,offset)

Parameter in mysqli_data_seek function

The parameters of the function are: 

Parameter Description
result It specifies the result set identifier returned by mysqli_query().
offset It specifies the field offset.

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();

  }

 

$qry="SELECT id,name,salary FROM emp order by name";

if ($result=mysqli_query($con,$qry))

  {

  mysqli_data_seek($result,0);

  $row=mysqli_fetch_row($result);

  printf ("ID: %s </br>Name: %s\n", $row[0], $row[1]);

  mysqli_free_result($result);

}

mysqli_close($con);

?>


Output

mysqli-data-seek-function-in-php.gif


Similar Articles