How to Get All Rows in PHP MySQL?

03-Apr-2023

.

Admin

How to Get All Rows in PHP MySQL?

Hi Dev,

In this post, we will learn how to get all rows in php mysql. i’m going to show you about get all mysql selected rows into an array. this post will give you simple example of how can i get total number of rows in php mysql. let’s discuss about how can i get multiple rows from database in php.

The mysqli_fetch_array() function is used to fetch rows from the database and store them as an array,associative arrays are the arrays where the indexes are the names of the individual columns of the table

Example:


index.php

<?php

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

// Check connection

if (mysqli_connect_errno()) {

echo "Database connection failed.";

}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname";

$result -> $mysqli -> query($sql);

// Numeric array

$row = mysqli_fetch_array($conn, MYSQLI_NUM);

printf ("%s (%s)\n", $row[0], $row[1]);

printf("\n");

// Associative array

$row = mysqli_fetch_array($conn, MYSQLI_ASSOC);

printf ("%s (%s)\n", $row["Firstname"], $row["Lastname"]);

mysqli_close($conn);

?>

Output:

A B

C D

E F

G H

A B

C D

E F

G H

I hope it could help you...

#MySQL

#PHP