2019/09/27

PHP and MySQL - A quick experiment on "mysqli_fetch_assoc()", "mysqli_fetch_row()", and "mysqli_fetch_array()".

This is a quick experiment on "mysqli_fetch_assoc()", "mysqli_fetch_row()", and "mysqli_fetch_array()".

As for which one to use, I think below comment from stackoverflow.

"I'd typically use _fetch_assoc, because I want to be accessing my data by name because it makes the code more comprehensible and less prone to errors. _fetch_array returns numerically and string indexed arrays, which is superfluous and potentially maybe possibly more wasteful. Not that it really matters in the grand scheme of things, but still. Do what you need; if you want named keys, use the function that gives you named keys and nothing more."

mysqli_fetch_assoc

This function will return a row as an associative array where the column names will be the keys storing corresponding value.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

$conn = mysqli_connect("localhost", "root", "", "sensors");

$query = "SELECT * FROM t_viewname";

// Get the number of row in the result
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $query));

echo "No. of Row: ";
echo $number_filter_row;
echo ", ";

// mysqli_query() -> Perform queries against the database.
// For example:
//      mysqli_query($con,"SELECT * FROM Persons");
//      mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

$result = mysqli_query($conn, $query);

$row = mysqli_fetch_assoc($result);

echo $row['dev_id'];
echo ', ';
echo $row['b_level'];
echo ', ';
echo $row['dev_location'];
echo '.';

?>

The Result


--------------------------------------------------------------------------------------------------------------------------

mysqli_fetch_row

This function will return a row where the values will come in the order as they are defined in the SQL query, and the keys will span from 0 to one less than the number of columns selected.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

$conn = mysqli_connect("localhost", "root", "", "sensors");

$query = "SELECT * FROM t_viewname";

// Get the number of row in the result
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $query));

echo "No. of Row: ";
echo $number_filter_row;
echo ", ";

// mysqli_query() -> Perform queries against the database.
// For example:
//      mysqli_query($con,"SELECT * FROM Persons");
//      mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

$result = mysqli_query($conn, $query);

$row = mysqli_fetch_row($result);

echo $row[0];
echo ', ';
echo $row[1];
echo ', ';
echo $row[2];
echo '.';

?>

The Result


--------------------------------------------------------------------------------------------------------------------------

mysqli_fetch_array

This function will actually return an array with both the contents of mysql_fetch_row and mysql_fetch_assoc merged into one. It will both have numeric and string keys which will let you access your data in whatever way you'd find easiest.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

$conn = mysqli_connect("localhost", "root", "", "sensors");

$query = "SELECT * FROM t_viewname";

// Get the number of row in the result
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $query));

echo "No. of Row: ";
echo $number_filter_row;
echo ", ";

// mysqli_query() -> Perform queries against the database.
// For example:
//      mysqli_query($con,"SELECT * FROM Persons");
//      mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

$result = mysqli_query($conn, $query);

$row = mysqli_fetch_array($result);

echo $row['dev_id'];
echo ', ';
echo $row['b_level'];
echo ', ';
echo $row['dev_location'];
echo '. <br>';

echo $row[0];
echo ', ';
echo $row[1];
echo ', ';
echo $row[2];
echo '.';

?>

The Result


Reference:

What's the difference between mysqli_fetch_array and mysqli_fetch_assoc in PHP?
https://www.quora.com/Whats-the-difference-between-mysqli_fetch_array-and-mysqli_fetch_assoc-in-PHP

No comments:

Post a Comment