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.
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.
Learn PHP PDO prepared statements with MySQL: connect safely, insert, select, update and delete, plus LIKE, IN lists, LIMIT, transactions and SQL injection.
0%