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.
Two HTML elements give you a keyboard-friendly accordion with no JavaScript. Open one at a time with name, swap the arrow for +/−, animate it, and fix the screen reader trap.
Find duplicates with GROUP BY and HAVING, preview them with ROW_NUMBER(), then delete all but one with a self-join. Error 1093, NULLs, case and spaces, and a UNIQUE key.
structuredClone(obj) makes a true deep copy that keeps Dates, Maps, Sets and circular references. When spread, Object.assign and JSON.stringify break, and how to copy classes.
Path("x").is_file() checks for a file, is_dir() for a folder and exists() for either. Plus create-if-missing, delete-if-present, wildcards, empty files and race conditions.
0%