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...
- How to Increase max_connections in MySQL?
- How to Install the PHP PDO MySQL Extension on Ubuntu 22.04?
- How to Install PHP MySQL on Linux Ubuntu?
- How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu?
- How to Change MySQL Root User Password Ubuntu?
- PHP 8 MySQL Form Validation Example
- How to Select First Row Only in PHP MySQL?
- How To Backup MySQL Database Using PHP Example