I need one row from a database. I don’t want an array of arrays. I just want that specific record.
Here is how you do it with PDO:
<?php
$row = $db->query('SELECT * FROM users WHERE id = 1')->fetch();
echo $row['name'];
?>
That’s it. query() runs the SQL, fetch() grabs the first row, and you access the data by column name.
Don’t use query() with user input
The example above is fine for a hardcoded ID. It breaks the moment you start pulling data from $_GET or $_POST.
If you have user input, stop using query(). Switch to prepared statements. This protects you from SQL injection and keeps your code cleaner.
<?php
$stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['name'];
?>
I used to mix these up early on. I’d write a query() with string concatenation because it felt faster to type. Then I’d spend hours debugging why someone’s email with an apostrophe broke the app. Prepared statements are the default now. If data comes from outside your code, use them.
Handle the case where no row exists
This trips people up constantly. fetch() doesn’t throw an error if it finds nothing. It returns false.
If you ignore that, you get warnings when you try to access array keys on a boolean. Check for it explicitly.
<?php
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row === false) {
// Handle the missing user
echo "User not found";
return;
}
echo $row['name'];
?>
It’s a small check. But it saves you from that annoying “Trying to access array offset on value of type bool” warning in your error log.
Choose your fetch mode
By default, PDO gives you a weird hybrid array. Every column appears twice: once by name, once by number index. It’s messy and wastes memory if you don’t need both.
You almost always want PDO::FETCH_ASSOC. This gives you an associative array where the keys are your column names.
You can pass this to every fetch() call, or set it once on the connection so you never have to think about it again. I prefer setting it once. It removes a mental step from every query.
<?php
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
?>
Now fetch() always returns an associative array by default. Your code gets shorter and less repetitive.
Use named placeholders when it makes sense
Positional placeholders (?) work fine for simple queries. But if you have multiple parameters, or the query is complex, named placeholders are easier to read.
<?php
$stmt = $db->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);
$row = $stmt->fetch();
?>
The :email in the SQL matches the key 'email' in the array. It’s self-documenting. You don’t have to count question marks to figure out which value goes where.
Get just one column
If you only need a single value, like a user ID or a timestamp, fetching the whole row is overkill.
Use fetchColumn(). It returns the first column of the next row directly as a scalar value. No array wrapping.
<?php
$name = $db->query('SELECT name FROM users WHERE id = 1')->fetchColumn();
echo $name;
?>
This is cleaner for simple lookups. I have a whole post on returning single values with PDO if you want to see more examples of when this shines.
Add LIMIT 1 for safety
If your WHERE clause doesn’t guarantee a unique result, add LIMIT 1.
This is a safety net. If your query accidentally matches five rows, fetch() will only grab the first one. Without the limit, you might think you’re getting “the” user, but you’re actually just getting an arbitrary one from a larger set.
It doesn’t fix bad queries. But it prevents silent bugs where your app behaves differently because it picked the wrong row.
Get an object instead of an array
Some people prefer accessing data as properties rather than array keys. PDO supports that too.
<?php
$row = $stmt->fetch(PDO::FETCH_OBJ);
echo $row->name;
?>
I use associative arrays more often because they’re standard in PHP and easier to debug with var_dump. But if your codebase leans heavily on objects, PDO::FETCH_OBJ keeps things consistent.
Never concatenate variables into SQL
This is the big one. I see this mistake in new code all the time.
<?php
// Don't do this:
$sql = "SELECT * FROM users WHERE id = " . $id;
$row = $db->query($sql)->fetch();
?>
It looks simple. It works until $id is 1 OR 1=1 or contains a quote that breaks your syntax.
Always use prepared statements with placeholders. Bind the variables separately from the SQL string. This forces PDO to handle the escaping and type safety for you. I wrote about why this stops SQL injection in detail, but the short version is: trust the driver, not your string concatenation.
Keep it simple
You don’t need a complex result set handler to get one row out of a database.
query() and fetch() handle the basics. Prepared statements handle the safety. A check for false handles the edge cases. That’s the whole toolkit.
Stop over-engineering this part of your code. Get the row, check if it exists, use the data. Move on to the logic that actually matters.