MySQLi (MySQL Improved) is a PHP extension for interacting with MySQL databases. It provides both procedural and object-oriented programming interfaces and offers a wide range of features for database operations. Below, I’ll cover some basic and advanced features of MySQLi along with examples.

Basic Features:

  1. Connection: To establish a connection to a MySQL database, use mysqli_connect() (procedural) or new mysqli() (object-oriented).

Procedural Example:

PHP
   $conn = mysqli_connect("localhost", "username", "password", "database_name");
   if (!$conn) {
       die("Connection failed: " . mysqli_connect_error());
   }

Object-Oriented Example:

PHP
   $conn = new mysqli("localhost", "username", "password", "database_name");
   if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }
  1. Query Execution: You can execute SQL queries using mysqli_query() for procedural style and $conn->query() for object-oriented style.

Procedural Example:

PHP
   $sql = "SELECT * FROM users";
   $result = mysqli_query($conn, $sql);

Object-Oriented Example:

PHP
   $sql = "SELECT * FROM users";
   $result = $conn->query($sql);
  1. Fetching Data: You can fetch data using functions like mysqli_fetch_assoc(), mysqli_fetch_row(), and others for the procedural style, or $result->fetch_assoc(), $result->fetch_row(), etc., for the object-oriented style.

Procedural Example:

PHP
   while ($row = mysqli_fetch_assoc($result)) {
       echo "Username: " . $row['username'] . "<br>";
   }

Object-Oriented Example:

PHP
   while ($row = $result->fetch_assoc()) {
       echo "Username: " . $row['username'] . "<br>";
   }
  1. Prepared Statements: MySQLi supports prepared statements for safe and efficient queries. Here’s an example of a prepared statement for selecting data:
PHP
   $sql = "SELECT username, email FROM users WHERE id = ?";
   $stmt = $conn->prepare($sql);
   $id = 1;
   $stmt->bind_param("i", $id); // "i" represents an integer
   $stmt->execute();
   $stmt->bind_result($username, $email);
   $stmt->fetch();

Advanced Features:

  1. Error Handling: MySQLi provides detailed error handling through functions like mysqli_error() and mysqli_errno() for procedural style and via $conn->error and $conn->errno for object-oriented style.
PHP
   if ($result === false) {
       echo "Error: " . mysqli_error($conn);
   }
  1. Transactions: MySQLi supports transactions. You can begin a transaction with mysqli_begin_transaction() (procedural) or $conn->begin_transaction() (object-oriented) and commit or rollback as needed.
PHP
   mysqli_begin_transaction($conn);
   // Perform database operations
   if (/* Operation successful */) {
       mysqli_commit($conn);
   } else {
       mysqli_rollback($conn);
   }
  1. Stored Procedures: You can call stored procedures using MySQLi, both for prepared statements and regular queries.
PHP
   $sql = "CALL procedure_name(?, ?)";
   $stmt = $conn->prepare($sql);
   $param1 = 1;
   $param2 = "example";
   $stmt->bind_param("is", $param1, $param2);
   $stmt->execute();
  1. Multiple Statements: MySQLi allows executing multiple SQL statements in a single call. Be cautious and validate input to prevent SQL injection in such cases.
PHP
   $sql = "INSERT INTO table1 VALUES ('value1'); INSERT INTO table2 VALUES ('value2')";
   $conn->multi_query($sql);
  1. Connection Handling: MySQLi supports managing multiple database connections and connection pooling.
PHP
   $conn1 = new mysqli("host1", "user", "pass", "db1");
   $conn2 = new mysqli("host2", "user", "pass", "db2");
   // Perform operations with $conn1 and $conn2
   $conn1->close();
   $conn2->close();

This is just an overview of some basic and advanced features of MySQLi. In real-world applications, you’ll often combine these features to interact with your database efficiently and securely. MySQLi is a powerful extension for PHP that is widely used for working with MySQL databases.

Leave a comment

Your email address will not be published. Required fields are marked *

Translate ยป