List All Project
Learn about the SELECT statement for reading data from the database and the foreach loop, which is used for looping over selected data and displaying it to the user.
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 ...