PHP CHAPTER 37

MySQL Select Data

SELECT retrieves records from a database table.

📘

What is it?

SELECT retrieves records from a database table.

💡

Why do we use it?

Applications need SELECT to display saved information.

⚙️

How does it work?

PHP executes a SELECT query and fetches the returned rows.

🧩

Syntax

PHP Syntax
SELECT id,name,email FROM students;
💻

Example

PHP Example
<?php

$conn = new mysqli("localhost", "root", "", "aifa_php_lab");

$result = $conn->query("SELECT id,name,email FROM students");

while ($row = $result->fetch_assoc()) {
    echo htmlspecialchars($row['name']) . " - ";
    echo htmlspecialchars($row['email']) . "<br>";
}

?>
🔍

Example Explained

query executes SELECT. fetch_assoc returns each row as an associative array.
🚀

More Examples

Select only students from a specific course using WHERE.

⚠️ Important Rules

Escape database content before placing it into HTML.

❌ Common Mistakes

Assuming rows always exist or displaying raw database content.

🌍 Real-World Use

Dashboards, profiles, reports and admin tables.

📝 Practice Exercise

Display all students in an HTML table.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →