PHP PDO fetch(), fetchAll() and FETCH_ASSOC Explained (Tested)
$stmt->fetch(PDO::FETCH_ASSOC) returns one row as an array, fetchAll() returns them all and fetchColumn() returns one value. Tested fetch modes, JOIN key clashes and the rowCount() trap.
Notes
Security notes, SOC lab write-ups and tested code by Mahir Faysal, a SOC Level 1 cybersecurity learner in Bangladesh, plus notes from my Next.js and Gemini projects. Sample posts are marked and drafts stay in the admin area.
$stmt->fetch(PDO::FETCH_ASSOC) returns one row as an array, fetchAll() returns them all and fetchColumn() returns one value. Tested fetch modes, JOIN key clashes and the rowCount() trap.
usort($rows, fn($a, $b) => $a['grade'] <=> $b['grade']) sorts by one column. Descending, several columns, UK dates, case-insensitive names, keeping keys and array_multisort.
Wire Composer PSR-4 autoload in PHP: map App\ to src/App/, require vendor/autoload.php, dump-autoload, and fix the usual class-not-found mistakes.
Use Europe/London in PHP the safe way: date_default_timezone_set, DateTimeImmutable, GMT vs BST, store UTC, display dd/mm/YYYY, and avoid DST surprises.
Validate PHP uploads with UPLOAD_ERR_OK, finfo MIME detection, size limits and random names — and ignore $_FILES type and the original filename.
A tested PHP login with sessions and PDO: password_verify, session_regenerate_id on success, a protected dashboard, and a logout that clears the session cookie.
0%