MySQLi Tutorial MySQLi References

MySQLi - Introduction



PHP works with wide range of database system, including Oracle and Sybase but MySQL is most commonly used database system with PHP.

MySQL

MySQL is an open-source relational database management system (RDBMS). A relational database organizes data into data tables in which data types may be related to each other. These relations help structure the data. SQL is a language programmers use to create, modify and extract data from the relational database, as well as control user access to the database. In addition to relational databases and SQL, MySQL works with an operating system to implement a relational database in a computer's storage system, manages users, allows for network access and facilitates testing database integrity and creation of backups.

MySQL is free and open-source software under the terms of the GNU General Public License, and is also available under a variety of proprietary licenses. MySQL has stand-alone clients that allow users to interact directly with a MySQL database using SQL, but more often, MySQL is used with other programs to implement applications that need relational database capability.

Downloading MySQL Database

MySQL can be downloaded for free from here: https://www.mysql.com.

Using PHP with MySQL

PHP combined with MySQL are cross-platform. The combination of PHP and MySQL gives unmet options to create any scale of website.

The PHP MySQLi extension is one of the most common method of using MySQL with PHP. It allows you to access MySQL database servers. It is designed to work with MySQL version 4.1 and above.

MySQLi

The MySQLi extension allows you to access MySQL database servers. It is designed to work with MySQL version 4.1 and above. It is also known as MySQL improved extension.

MySQLi extension provides both object oriented and procedural approach to handle database operations.

Object Oriented Interface

<?php
//establishing connection 
$mysqli = new mysqli("localhost", "user", "password", "database_name");

//checking connection
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//closing the connection
$mysqli->close();
?>

Procedural Approach

<?php
//establishing connection 
$mysqli = mysqli_connect("localhost", "user", "password", "database_name");

//checking connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//closing the connection
mysqli_close($mysqli);
?>

Along with this, it also provides various others features including:

  • Supports prepared statements
  • Supports multiple statements
  • Supports transactions
  • Provides enhanced debugging capabilities

Installation & Runtime Configuration

The MySQLi extension was introduced with PHP version 5.0.0. The MySQL Native Driver was included in PHP version 5.3.0.

For installation details, go to: https://php.net/manual/en/mysqli.installation.php

For runtime configuration details, go to: https://php.net/manual/en/mysqli.configuration.php