Wednesday, June 26, 2024
HomePHPmysqli_fetch_array PHP Perform - Phppot

mysqli_fetch_array PHP Perform – Phppot


by Vincy. Final modified on April ninth, 2024.

This can be a guide to study concerning the PHP mysqli_fetch_array perform. It describes the syntax, parameters, return values, and a few utilization practices of this methodology.

It’s an unskippable perform when constructing database-related modules.

mysqli_fetch_array syntax

The mysqli_fetch_array is a pre-built perform in PHP. That is for fetching a row from the database consequence set. Every time calling this perform, it returns the subsequent row from the consequence set subsequently.

Object-oriented model

public mysqli_result::fetch_array(int $mode = MYSQLI_BOTH): array|null|false

Procedural model

mysqli_fetch_array(mysqli_result $consequence, int $mode = MYSQLI_BOTH): array|null|false

Description

The mysqli_result::fetch_array and mysqli_fetch_array offers the identical outcomes. It varies within the model of calling by object-oriented or procedural strategies.

The $result’s the prime reference used to fetch the row which is the item representing the consequence set. The relevant parameters and return values of those capabilities are listed under.

Parameters

$consequence – It’s a database useful resource object that represents the consequence set. The consequence set is created by PHP mysqli_query. This argument applies to the procedural model solely. Within the object-oriented model the fetch_array is invoked upon this $consequence object.

$mode – It’s for specifying what kind of array (numeric, associative, or each) to be returned. The potential values are the PHP MYSQL constants MYSQLI_NUM, MYSQLI_ASSOC and MYSQL_BOTH(default). It’s an non-compulsory parameter.

Return values

This methodology returns a row from the consequence set. It returns null if no extra rows within the consequence set. If the fetch course of is failed, then it’ll return false.

MySQL Brand Image

PHP mysqli_fetch_array instance

This tutorial offers two examples of fetching the database rows through each the procedural and object-oriented types.

Each examples print the resultant array utilizing the PHP print assertion. You possibly can substitute the print assertion to deal with the row knowledge to show in a desk view or to carry out any manipulations.

 

php brand logo

mysqli_fetch_array PHP procedural instance

<?php
// Create a MySQL database connection
$connection = mysqli_connect("localhost", "root", "", "db_profile");

$question = "SELECT * FROM tbl_user";
// Get consequence set
$consequence = mysqli_query($connection, $question);

// Fetching subsequent row from the consequence set utilizing mysqli_fetch_array()
$row = mysqli_fetch_array($consequence);

print "<PRE>";
print_r($row);

mysqli_free_result($consequence);

// Closing connection
mysqli_close($connection);
?>

mysqli_fetch_array php

fetch_array PHP object-oriented instance

<?php
$mysqli = new mysqli("localhost", "root", "", "db_profile");

$question = "SELECT * FROM tbl_user";
$consequence = $mysqli->question($question);

$row = $result->fetch_array();

print "<PRE>";
print_r($row);
?>

Extra about mysqli_fetch_array

  1. What’s the usage of mysqli_fetch_array ()?
  2. Easy methods to print mysql_fetch_array in PHP?
  3. What’s the results of Fetch_array in PHP?
  4. Was mysql_fetch_array eliminated within the 7.0 PHP model?

What’s the usage of mysqli_fetch_array ()?

The resultant row is an array with each the sphere offset and area names as its indices.

The numerical indices are helpful on the time of accessing a number of columns with the identical identify with out overriding. The under picture reveals the database rows and the mysqli_fetch_array() perform’s return array. The output array accommodates each the numeric and associative array parts.

Easy methods to print mysqli_fetch_array() in PHP?

Within the above PHP examples, it prints the array results of mysqli_fetch_array().

This PHP instance reveals the way to print the row returned by mysqli_fetch_array(). It prints the row knowledge as comma-separated values by operating this perform in a PHP loop.

<?php
// Create a MySQL database connection
$connection = mysqli_connect("localhost", "root", "", "db_profile");

$question = "SELECT * FROM tbl_user";
// Get consequence set
$consequence = mysqli_query($connection, $question);

// Fetching rows in a loop
whereas($row = mysqli_fetch_array($consequence, MYSQLI_NUM)){
    print implode(", ", $row) . "<br/>";
}

mysqli_free_result($consequence);

// Closing connection
mysqli_close($connection);
?>

Output:

1, Kevin, Thomas, Google
2, Tim, Martin, Tesla

PHP supplies a perform mysqli_fetch_all() to get all of the rows in a single name. However, it isn’t a good suggestion if the database is giant.

Reminiscence efficiency-wise  mysqli_fetch_array() is one of the best in comparison with the mysql_fetch_all(). The latter will have an effect on the efficiency if the fetch is handled the big file set.

The resultant array may be filtered additional by making use of PHP situations contained in the loop.

What’s the results of Fetch_array in PHP?

PHP fetch_array perform ends in the identical array because the mysqli_fetch_array(). The distinction is that it’s an object-oriented means of fetching the database rows.

It wants the MySQLi occasion to be created with the database credentials as proven within the under instance. Then, that occasion is used to get the consequence set. The PHP fetch_array known as upon this consequence set object.

By including the ORDER BY clause to the question, it’ll fetch the sorted outcomes from the database

Was mysql_fetch_array eliminated within the 7.0 PHP model?

The mysql_ extension may be very outdated and deprecated as of the model PHP 5.5.0. Then, the backward compatibility can be eliminated within the later model 7.0.0.

The mysqli_fetch_array is the substitute of this deprecated mysql_fetch_array perform.

The PDOStatement::fetch() can be a solution to fetch the subsequent row from the consequence.

Vincy
Written by Vincy, an online developer with 15+ years of expertise and a Masters diploma in Laptop Science. She makes a speciality of constructing trendy, light-weight web sites utilizing PHP, JavaScript, React, and associated applied sciences. Phppot helps you in mastering net improvement by over a decade of publishing high quality tutorials.

↑ Again to High

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments