What is the correct way to fetch data from a MySQL database using PHP's mysqli_fetch_object() function?

When fetching data from a MySQL database using PHP's mysqli_fetch_object() function, you need to first establish a connection to the database, execute a query to retrieve the data, and then use mysqli_fetch_object() to fetch each row of the result set as an object. The fetched object will have properties corresponding to the columns in the result set.

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

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

// Execute a query to retrieve data
$result = mysqli_query($connection, "SELECT * FROM table");

// Fetch data as objects
while ($row = mysqli_fetch_object($result)) {
    // Access object properties
    echo $row->column_name;
}

// Close the connection
mysqli_close($connection);