Mysql 3D logo
|

Understanding mysqli_fetch_array in PHP

Introduction

In the realm of PHP and MySQL, efficient data retrieval is essential for building dynamic web applications. One crucial function that facilitates this process is mysqli_fetch_array(). In this post, we’ll delve into what mysqli_fetch_array() is, how it works, and practical examples of its usage.

What is mysqli_fetch_array()?

mysqli_fetch_array() is a PHP function used to fetch a row from a MySQL result set as an associative array, a numeric array, or both. This means you can access the fetched data using both column names and numerical indices.

Syntax

mysqli_fetch_array($result, $resulttype);
  • $result: The result set obtained from a MySQL query.
  • $resulttype: An optional parameter that determines the type of array to return (MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH).

Understanding Result Types

  • MYSQLI_ASSOC: Returns an associative array with column names as keys.
  • MYSQLI_NUM: Returns a numeric array with column indices as keys.
  • MYSQLI_BOTH: Returns both an associative and a numeric array.

Usage Examples

Example 1: Fetching an Associative Array

Fetching an associative array in PHP involves retrieving data organized with named keys. Unlike numeric arrays, where data is indexed by numbers, associative arrays use descriptive labels to access information. This method is especially handy when dealing with complex or structured data that requires specific identification. By using these keys, we can easily retrieve and manipulate the elements within the array. This concept is vital for tasks like managing databases or handling JSON responses, making it an essential skill for PHP developers.

<?php
$conn = new mysqli("localhost", "username", "password", "database");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Example 2: Fetching a Numeric Array

Fetching a numeric array in PHP means retrieving data organized by numerical indexes. Each piece of information is stored with a unique number, starting from zero. This method is crucial for tasks like managing lists or tables efficiently. It’s a fundamental concept in PHP, essential for tasks from basic data storage to complex algorithms.

<?php
$conn = new mysqli("localhost", "username", "password", "database");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        echo "ID: " . $row[0]. " - Name: " . $row[1]. " - Email: " . $row[2]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Conclusion

mysqli_fetch_array() is a versatile PHP function that allows you to retrieve data from a MySQL result set in various ways. By understanding its usage and result types, you can efficiently handle data in your PHP applications. Whether you prefer associative arrays, numeric arrays, or a combination of both, this function provides the flexibility you need to work with your MySQL data effectively.

Photo by Rubaitul Azad on Unsplash

Similar Posts