//connect to database $conn = mysqli_connect("localhost", "user", "password", "database"); //execute query and store results in $result variable $result = mysqli_query($conn, "SELECT * FROM table"); //loop through result set and fetch each row as an object while ($row = mysqli_fetch_object($result)) { echo $row->column1. " " . $row->column2. "
"; }
//connect to database $dsn = 'mysql:host=localhost;dbname=database'; $username = 'user'; $password = 'password'; try { $conn = new PDO($dsn, $username, $password); } catch (PDOException $e) { echo $e->getMessage(); } //execute query and store results in $result variable $result = $conn->query("SELECT * FROM table"); //loop through result set and fetch each row as an object while ($row = $result->fetchObject()) { echo $row->column1. " " . $row->column2. "In both examples, the fetch_object() method is called on the result set to retrieve the next row as an object. The object properties correspond to the column names in the database table. The package/library used in the examples above are the MySQLi Extension and PDO Extension, which are built-in database extensions in PHP.
"; }