JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Loop PHP MySQLi Get Array Function

Author:JIYIK Last Updated:2025/04/21 Views:

MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLiwith queries.

In this article, we will see mysqli_fetch_array()the use of functions and methods to iterate over data.

This extension was introduced in PHP version 5.0.0 and is designed to work with MySQL 4.1.13 or higher. We use PHP version 7.4.1 and phpMyAdmin in this tutorial.

You can download XAMPP from its official website. (If you have XAMPP, you don't have to install MySQL and PHP separately).


Iteration mysqli_fetch_array()Function

mysqli_fetch_array()It is used to $resultretrieve the data of the current row from the database using as the first parameter, saving the output as an associative array, a numeric array, or both (depending on the second parameter). Now, we have the following data in our student database.

mysqli_fetch_array()Function usage MYSQLI_NUMmode

Let us write the following program to read data from the student database mysqli_fetch_array()named using function db_students. The following code will connect to the database and display a failure message in case of failure.

After successfully connecting to the database, it will mysqli_queryread the records using the function and save them $resultinto the variable. mysqli_fetch_array()This resulting variable will be used and MYSQLI_NUM(it behaves like the mysqli_fetch_row() function) as an argument to display the current row as an array of numbers with indices ranging from 0to n-1.

Sample code:

<?php
	$host = "localhost";
    $username = "root";
    $password = "";
	$database = "db_students";
	$connection = mysqli_connect($host, $username, $password, $database); 
   
    if (mysqli_connect_errno()) {
        echo "Database connection failed."; 
    }

    $sql = "SELECT * FROM tb_students";
	$result = mysqli_query($connection, $sql);
    $row = mysqli_fetch_array($result,MYSQLI_NUM);
    print_r($row)
?>

Output:

mysqli_fetch_array()Function usage MYSQLI_ASSOCmode

Here, due to the parameters MYSQLI_ASSOC, mysqli_fetch_array()the function behaves similar to mysqli_fetch_assoc()and the column names of the table will be displayed as indices of the array. Practice the following code and observe the output.

Sample code:

<?php
	
    $host = "localhost";
    $username = "root";
    $password = "";
	$database = "db_students";
	$connection = mysqli_connect($host, $username, $password, $database); 
   
    if (mysqli_connect_errno()) {
        echo "Database connection failed."; 
    }

    $sql = "SELECT * FROM tb_students";
	$result = mysqli_query($connection, $sql);
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    print_r($row)
?>

Output:

mysqli_fetch_array()Function usage MYSQLI_BOTHmode

Using the function with parameter MYSQLI_BOTHwill mysqli_fetch_array()store the data into an array which we can access using column name and column index. Practice the following code and see the output.

Sample code:

<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = "db_students";
    $connection = mysqli_connect($host, $username, $password, $database); 

    if (mysqli_connect_errno()) {
        echo "Database connection failed."; 
    }

    $sql = "SELECT * FROM tb_students";
    $result = mysqli_query($connection, $sql);
    $row = mysqli_fetch_array($result,MYSQLI_BOTH);
    print_r($row)
?>

Output:

Loop mysqli_fetch_array()Function

We will use the following code to iterate mysqli_queryand compare the output with the student table records.

Remember that you can loop over all patterns of mysqli_fetch_array(), MYSQLI_ASSOC, , MYSQLI_NUMand . When using , you must pass it as a parameter.MYSQLI_BOTHmysqli_fetch_array()

<?php
    $host = "localhost";
    $username = "root";
    $password = "";
	$database = "db_students";
	$connection = mysqli_connect($host, $username, $password, $database); 
   
    if (mysqli_connect_errno()) {
        echo "Database connection failed."; 
    }

    $sql = "SELECT * FROM tb_students";
	$result = mysqli_query($connection, $sql);
        
    $std_num=0;
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        echo "Student Number ".$std_num."<br>";
        echo "ID: ".$row['ID']."<br>";
        echo "First Name: ".$row['FIRST_NAME']."<br>";
        echo "Last Name: ".$row['LAST_NAME']."<br>";
        echo "Age: ".$row['AGE']."<br>";
        echo "<br><br>";
    	$std_num++;
    }
?>

Output:

You can view ID, FIRST_NAME, LAST_NAME, and compare AGEthem to the following table named .tb_students


in conclusion

The above discussion concludes that mysqli_fetch_array()functions help us to retrieve data from the database.

Depending on our needs and requirements, we can use its output mode, whether to access values ​​by column index or column name, or both. We can then loop through the results to view each record of the table.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Generate a random and unique string in MySQL

Publish Date:2025/04/21 Views:77 Category:MySQL

Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5() , RAND() , , SUBSTR() and UUID() . There is no inbuilt method to generate random string in MySQL, but there are many

Changing MySQL root password on Mac

Publish Date:2025/04/21 Views:160 Category:MySQL

This article teaches you how to change the MySQL user password on OS X. root We will be using XAMPP so that you can change the password using the MySQL console. Installing XAMPP for OSX First, download and install XAMPP for OSX from Apache

Using CURRENT_TIMESTAMP as a default value in MySQL

Publish Date:2025/04/21 Views:196 Category:MySQL

This article teaches you how to use as 5.6.5 in MySQL versions lower than . Thus, you can prevent MySQL error 1293. CURRENT_TIMESTAMP DEFAULT Our approach involves reordering table columns and using DEFAULT 0 and time values. Reproduce the

If ELSE in MySQL

Publish Date:2025/04/11 Views:86 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:143 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial