So sánh mysqli_fetch_assoc and mysqli_fetch_array

Các lệnh mysql_fetch_assoc,mysql_fetch_array,mysql_fetch_object đều dùng để fetch dữ liệu từ câu query, tuy nhiên dữ liệu trả về sẽ có dạng khác nhau ứng với mỗi câu lệnh. Ví dụ:

$sql = mysql_query("SELECT * FROM table WHERE id=id");

+ mysql_fetch_assoc(): $rs = mysql_fetch_assoc($sql); Dữ liệu trả về sẽ có dạng:

Array( 'tên_field1'=>giá trị 1, 'tên_field2'=>giá trị 2, 'tên_field3'=>giá trị 3, );

Và 1 mảng như vậy gọi là associative array Hiển thị dữ liệu:

echo $rs['tên_field1'],$rs['tên_field2']

+ mysql_fetch_row(): $rs = mysql_fetch_row($sql); Dữ liệu trả về sẽ có dạng:

Array( 0=>giá trị 1, 1=>giá trị 2, 2=>giá trị 3, );

Và 1 mảng như vậy gọi là enumerated array Hiển thị dữ liệu

echo $rs[0],$rs[1]

+ mysql_fetch_array(): $rs = mysql_fetch_array($sql,mode_fetch); trong đó mode_fetch có các giá trị: + MYSQL_ASSOC: trả về associative array(giống mysql_fetch_assoc()) + MYSQL_NUM: trả về enumerated array(giống mysql_fetch_row()) +MYSQL_BOTH : (mặc định) Dữ liệu trả về sẽ có dạng:

Array( 'tên_field1'=>giá trị 1, 0=>giá trị 1, 'tên_field2'=>giá trị 2, 1=>giá trị 2, 'tên_field3'=>giá trị 3, 2=>giá trị 3, );

Hiển thị dữ liệu:

echo $rs['tên_field1'],$rs['tên_field2']; //hoặc echo $rs[0],$rs[1]; // 0,1 là thự tự của các field trong table

+ mysql_fetch_object(): $rs = mysql_fetch_object($sql); Dữ liệu trả về sẽ có dạng:

Object ( 'tên_field1'=>giá trị 1, 'tên_field2'=>giá trị 2, 'tên_field3'=>giá trị 3, );

Hiển thị dữ liệu:

echo $rs->tên_fiel1,$rs->tên_fiel2,..

In this article, we will see the mysqli_fetch_array() & mysqli_fetch_object() function in PHP. The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

mysqli_fetch_object() function: This function returns an object with properties that match the fetched row’s properties and advances the internal data pointer. We can only access the data by the specific field names, and not by their offsets. It returns an object containing properties that match the fetched row, or FALSE if no further rows are available.

Syntax:

mysqli_fetch_object(data);

Parameters:

  • data: The data resource that is being evaluated. This result comes from a call to mysqli_query() function.

Example: This example describes the mysqli_fetch_object() function that fetches the next row of a result set as an object.

Suppose we have table named students which have the following data values:

Create table:

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

Insert into the table:

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

After creating the table, the following table structure will appear.

student_id student_name 01 Abc 02 Xyz

$con

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

0

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

1

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

6

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

8

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

1

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2`$con`

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

4

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

5

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

6

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

7

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

8

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

01 Abc 02 Xyz

0`$con`

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

5

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

1

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2`$con`

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

4

mysqli_fetch_array(result, arrayType);

1

mysqli_fetch_array(result, arrayType);

2

mysqli_fetch_array(result, arrayType);

3

mysqli_fetch_array(result, arrayType);

4

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

mysqli_fetch_array(result, arrayType);

6

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

mysqli_fetch_array(result, arrayType);

8

mysqli_fetch_array(result, arrayType);

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

0

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

mysqli_fetch_array(result, arrayType);

8

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

3

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

8

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

mysqli_fetch_array(result, arrayType);

8

mysqli_fetch_array(result, arrayType);

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

8

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

3

Output:

01 Abc 02 Xyz

mysqli_fetch_array() function: It is used to fetch rows from the database and store them as an array. The array can be fetched as an associative array, as a numeric array, or both.

Syntax:

mysqli_fetch_array(result, arrayType);

Parameters:

  • result: The result of resource that is being evaluated. This result comes from a call to mysqli_query() function.
  • arrayType: The type of array that is to be fetched. It’s a constant and can take the following values: MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.

Example: This example describes the mysqli_fetch_array() function that returns an array that corresponds to the fetched row & moves the internal data pointer ahead.

Suppose we have table named students which have the following data values:

Create table:

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

Insert into the table:

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

After creating the table, the following table structure will appear.

student_id student_name 01 Abc 02 Xyz

PHP

$con

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

0

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

1

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

6

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

8

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

1

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2`$con`

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

4

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

5

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

6

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

7

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

8

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

01 Abc 02 Xyz

0`$con`

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

2

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

5

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

1

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2`$con`

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

4

mysqli_fetch_array(result, arrayType);

1

mysqli_fetch_array(result, arrayType);

2

mysqli_fetch_array(result, arrayType);

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

00

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

mysqli_fetch_array(result, arrayType);

6

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

5

mysqli_fetch_array(result, arrayType);

8

mysqli_fetch_array(result, arrayType);

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

06

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

07

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

08

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

10

mysqli_fetch_array(result, arrayType);

3

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

06

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

13

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

14

CREATE TABLE students (

student_id int(10),
student_name VARCHAR(20)
)

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

0

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

9

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

2

Insert into students(

student_id, student_name
) Values(01, 'Abc') Insert into Students(
student_id, student_name
) Values(02, 'Xyz')

3

Output:

01 Abc 02 Xyz

Difference between mysqli_fetch_array() and mysqli_fetch_object() Function:

mysqli_fetch_object() function: Fetch a result row as an object.

mysqli_fetch_array() function: Fetch a result row as a combination of associative array and regular array.

Let us see the differences in a tabular form -:

mysqli_fetch_array() mysqli_fetch_object() 1. It fetches a result row as an associative array and a numeric array. It is used to return the row of a result-set 2.

Its syntax is -:

$mysqli_result -> fetch_array(result_type)

Its syntax is -:

$mysqli_result -> fetch_object(classname, params)

3. It takes one parameter that is a result. It takes two parameters that are class name and array of parameters 4. Its return value is array of string. It returns an object with string properties for the fetched row. 5. It is supported in PHP version 5+ It is supported in PHP version 5+

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!