What is the correct syntax for retrieving data from a MySQL database using PHP?

When retrieving data from a MySQL database using PHP, you need to establish a connection to the database, execute a SQL query to select the data you want, and then fetch the results. The correct syntax involves using functions like mysqli_connect(), mysqli_query(), and mysqli_fetch_assoc().

<?php
// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Execute a SQL query to select data
$sql = "SELECT * FROM table_name";
$result = mysqli_query($connection, $sql);

// Fetch the results
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the data here
    }
} else {
    echo "No results found";
}

// Close connection
mysqli_close($connection);
?>