PHP and MySQL

Connect with PDO, and always use prepared statements.

PHP
<?php
$pdo = new PDO("mysql:host=localhost;dbname=school;charset=utf8mb4", "user", "password", [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare("SELECT id, name FROM students WHERE dept = ?");
$stmt->execute([$_GET["dept"] ?? "CSE"]);

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    echo htmlspecialchars($row["name"]) . "\n";
}

The ? keeps user input apart from the SQL, which stops SQL injection. Never join input into a query string.

Passwords

PHP
<?php
$hash = password_hash($password, PASSWORD_DEFAULT);
$ok = password_verify($password, $hash);
โ† Forms and output safetySessions and classes โ†’