Thuta Learning
AdvancedProgrammingbeginner

SELECT Data

Relax. We'll talk through this in plain words — no textbook voice.

A SELECT query is used to read data from the database. If you want to display things like a product list, blog posts, a user table, or contact messages, you need SELECT. For SELECT queries that involve user input, use a prepared statement.

php
<?php
$searchEmail = "user@example.com";

$stmt = $conn->prepare("SELECT id, name, email FROM contacts WHERE email = ?");
$stmt->bind_param("s", $searchEmail);
$stmt->execute();
$result = $stmt->get_result();

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

$stmt->close();
?>
You should see
Displays matching contact records by email, in name - email format.

Easy traps

  • Dropping user input directly into an SQL string can open the door to SQL injection.