// Connect to database $mysqli = new mysqli('localhost', 'username', 'password', 'my_database'); // Query the database $result = $mysqli->query('SELECT * FROM my_table'); // Store the result set $result->store_result(); // Loop through results while($row = $result->fetch_assoc()) { echo $row['name'] . ', ' . $row['age'] . '
'; }
// Connect to database $pdo = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password'); // Query the database $stmt = $pdo->prepare('SELECT * FROM my_table'); // Execute the query and store the result set $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); // Loop through results foreach($result as $row) { echo $row['name'] . ', ' . $row['age'] . 'In this example, we connect to a MySQL database using the PDO interface. We then prepare and execute a SELECT query, and store the result set in an array using the fetchAll() method. This method automatically buffers the entire result set in memory, so there is no need to use the store_result() function. Package/Library: The mysqli extension is included with PHP by default, and does not require any additional installation or configuration. It provides an object-oriented interface for working with MySQL databases. The PDO extension is also included with PHP by default, and provides a unified interface for working with various database systems, including MySQL. It does require the PDO MySQL driver to be installed, which is often included with the default PHP installation.
'; }