Executes SQL that does not return a result set and returns the number of affected rows.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Executes SQL that does not return a result set and returns the number of affected rows.
Syntax and Return Value
PDO::exec(Parameter #0 [ <required> string $statement ]): mixed
The return value and exceptional cases depend on the call described here: Do not use exec to fetch SELECT rows; do not interpolate untrusted values into SQL.
Example 1: Basic Use
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$affected = $pdo->exec("UPDATE products SET price = price + 48 WHERE id = 1");
echo "Updated rows: " . $affected;
Example 2: A Second Scenario
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$affected = $pdo->exec("UPDATE products SET price = price + 62 WHERE id = 2");
echo "Updated rows: " . $affected;
Example 3: Practical Checks
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$affected = $pdo->exec("UPDATE products SET price = price + 79 WHERE id = 3");
echo "Updated rows: " . $affected;
Important Behavior and Edge Cases
Do not use exec to fetch SELECT rows; do not interpolate untrusted values into SQL.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDO::exec | Executes SQL that does not return a result set and returns the number of affected rows. |
| Related API / pattern | prepare with bound parameters is appropriate for untrusted values. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Do not use exec to fetch SELECT rows; do not interpolate untrusted values into SQL. |
| Using a related API interchangeably | prepare with bound parameters is appropriate for untrusted values. |
Key Takeaways
- Executes SQL that does not return a result set and returns the number of affected rows.
- Do not use exec to fetch SELECT rows; do not interpolate untrusted values into SQL.
- prepare with bound parameters is appropriate for untrusted values.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.