How can the number of database records be determined in PHP?

To determine the number of database records in PHP, you can execute a SQL query to count the number of rows in the database table. This can be achieved using the COUNT() function in SQL. Once the query is executed, you can fetch the result and display the total number of records.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to count the number of records
$sql = "SELECT COUNT(*) as total_records FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output the total number of records
    $row = $result->fetch_assoc();
    echo "Total Records: " . $row['total_records'];
} else {
    echo "0 records found";
}

$conn->close();
?>