//Connect to the database $conn = mysqli_connect('localhost', 'username', 'password', 'mydatabase'); //Query to retrieve all records from "users" table $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); //Loop through the result set and display each record while($row = mysqli_fetch_assoc($result)){ echo $row['username'] . "
"; } //Close the database connection mysqli_close($conn);
//Connect to the database using PDO $conn = new PDO("mysql:host=localhost;dbname=mydatabase", 'username', 'password'); //Query to retrieve all records from "products" table $sql = "SELECT * FROM products"; $stmt = $conn->query($sql); //Fetch all records and display them $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($rows as $row){ echo $row['name'] . "In this example, we connect to a MySQL database using PDO, retrieve all records from the "products" table, fetch all records using fetchAll() method, and then display each record's name. Package Library: PDO_MySQL
"; } //Close the database connection $conn = null;