MySQLi - Introduction



MySQLi is an extension to MySQL API available in PHP and is introduced from PHP 5.0 onwards. It is also known as MySQL improved extension. Motivation behind MySQLi was to take advantage of new features available in MySQL 4.1.3 onwards. It provides numerous benefits over MySQL extension.

  • MySQL provides an object oriented interface. It provides both object oriented and procedural approach to handle database operations.

Object Oriented Interface

<?php
   $mysqli = mysqli_connect("localhost", "user", "password", "database-name");

   $result = mysqli_query($mysqli, "SELECT 'Welcome to MySQLi' AS _msg FROM DUAL");
   $row = mysqli_fetch_assoc($result);
   echo $row['_msg'];
?>

Procedural Approach

<?php
   $mysqli = new mysqli("localhost", "user", "password", "database-name");

   $result = $mysqli→query("SELECT 'Welcome to MySQLi' AS _msg FROM DUAL");
   $row = $result→fetch_assoc();
   echo $row['_msg'];
?>
  • MySQLi supports prepared statments.

  • MySQLi supports multiple statments.

  • MySQLi supports transactions.

  • MySQLi provides enhanced debugging capabilities.

Advertisements