// connect to database $mysqli = new mysqli("localhost", "username", "password", "database"); // execute query $resultSet = $mysqli->query("SELECT * FROM users"); // loop through result set and retrieve data as object while ($row = $resultSet->fetch_object()) { echo $row->name; echo $row->email; }
// connect to database $mysqli = new mysqli("localhost", "username", "password", "database"); // prepare and execute stored procedure $stmt = $mysqli->prepare("CALL get_users()"); $stmt->execute(); // loop through result set and retrieve data as object $resultSet = $stmt->get_result(); while ($row = $resultSet->fetch_object()) { echo $row->name; echo $row->email; }In both examples, fetch_object is used to retrieve each row of data as an object, which can then be accessed using object syntax. This function is part of the mysqli class, which is a package library for accessing MySQL databases.