PDO query()
Use query() for simple SELECT statements without user input. For any user-provided data, always use prepared statements instead.
Use query() for simple SELECT statements without user input. For any user-provided data, always use prepared statements instead.
<?php
$pdo = createConnection();
// Simple query (no user data — safe)
$stmt = $pdo->query("SELECT * FROM categories ORDER BY name");
$categories = $stmt->fetchAll();
// Row count
echo $stmt->rowCount();
// Single row
$stmt = $pdo->query("SELECT COUNT(*) as total FROM users");
$row = $stmt->fetch();
echo $row["total"];
Never use query() with any string that includes user input — use prepare()/execute() instead.