$query = "SELECT * FROM users WHERE id = 1"; $result = $mysqli->query($query); $user = $result->fetch_object(); echo $user->name;
$query = "SELECT * FROM books WHERE author = 'J.K. Rowling'"; $result = $pdo->query($query); while ($book = $result->fetch_object()) { echo $book->title; }In this example, the code retrieves a list of books written by J.K. Rowling from the `books` table of a database, and uses a `while` loop to iterate over the result set and fetch each book as an object. Then it uses the `->` notation to access the `title` property of each `$book` object. This code is using the PDO package library.