List All Project
Explore how to retrieve and display all project records in a PHP and MySQL CRUD application. This lesson guides you through establishing a PDO database connection, executing SELECT queries, and using loops to render project titles dynamically on a web page.
We'll cover the following...
We'll cover the following...
Let’s create a file called project_list.php at the root of our project, and add some basic HTML code into it. We’ll add our PHP code in the body tag.
First things first, let’s add the connection to the database and the try/catch block:
<?php
try {
require "./config.php";
$connection = new PDO($dsn, $username, $password, $options);
} catch (PDOException $err) {
echo $err->getMessage();
}
?>
Read from the database
Inside the try block, under the $connection variable, we’ll add our SELECT SQL query:
$sql = 'SELECT * FROM projects ORDER BY title';
$statement = $connection->query($sql);
We’ll execute the SQL query with the ...