Reading Data from the Database
Explore how to read data efficiently from a PHP database using PDO. Understand crafting select queries with named placeholders, preparing and executing queries, binding parameters, fetching data as objects or arrays, and displaying results conditionally. This lesson helps you gain practical skills in securely retrieving and managing database records in PHP.
In this lesson, we’ll learn about the different phases of reading data from the database. We’ll learn how to prepare queries, bind parameters, define bound values, and execute queries.
Step 1: Write a query
Write a regular select statement and instead of values, put named placeholders. For example:
$sql = "SELECT * FROM users WHERE city = :city";
Step 2: Prepare the query to execute
Prepare the query:
$query = $dbh -> prepare($sql);
Bind the parameters: ...