What is the purpose of using COUNT(*) and ORDER BY RAND() in PHP MySQL queries?

Using COUNT(*) in a MySQL query allows us to count the number of rows in a table, while ORDER BY RAND() randomizes the order of the results. This can be useful for scenarios where we want to retrieve a random row from a table or display a random set of rows to the user.

// 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 get a random row from a table
$sql = "SELECT * FROM table_name ORDER BY RAND() LIMIT 1";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Output the random row
        echo "Random Row: " . $row["column_name"];
    }
} else {
    echo "0 results";
}

$conn->close();