Table Prefix
Set once – applied automatically to every table name in every query.
What is a table prefix?
A table prefix is a short string that gets prepended to every table name in your queries.
It is common in shared hosting environments or CMS systems like WordPress (wp_)
where multiple applications share one database.
Without a prefix you always write the full table name. With a prefix set, you only write the base name – PDOdb adds the prefix automatically:
// Without prefix
$users = $db->get('wp_users');
// → SELECT * FROM wp_users
// With prefix 'wp_' set
$users = $db->get('users');
// → SELECT * FROM wp_users
Setting the prefix
Pass the prefix in the constructor:
$db = new PDOdb([
'host' => 'localhost',
'username' => 'user',
'password' => 'secret',
'db' => 'my_database',
'prefix' => 'wp_',
]);
Or set / change it at any time after initialization:
$db->setPrefix('app_');
// Read the current prefix
$prefix = $db->getPrefix(); // → 'app_'
Where the prefix is applied
The prefix is automatically added in all of the following:
$db->setPrefix('tst_');
// get(), getOne(), getValue()
$db->get('users');
// → SELECT * FROM tst_users
// insert(), update(), delete(), replace()
$db->insert('orders', ['user_id' => 1, 'total' => 49.99]);
// → INSERT INTO tst_orders (user_id, total) VALUES (?, ?)
// join()
$db->join('orders o', 'o.user_id = users.id', 'LEFT')->get('users');
// → SELECT * FROM tst_users LEFT JOIN tst_orders o ON o.user_id = users.id
// paginate()
$db->paginate('users', 1);
// → SELECT SQL_CALC_FOUND_ROWS * FROM tst_users LIMIT 0, 20
// tableExists()
$db->tableExists('users');
// → checks for tst_users in information_schema
Prefix and subqueries
Subquery objects created via subQuery() do not inherit
the prefix automatically. Use the full table name inside a subquery:
$db->setPrefix('tst_');
$sub = $db->subQuery();
// ❌ Wrong – prefix is not applied inside subQuery()
$sub->where('status', 'active')->get('orders', null, 'user_id');
// ✅ Correct – use the full table name
$sub->where('status', 'active')->get('tst_orders', null, 'user_id');
$rows = $db->where('id', $sub, 'IN')->get('users');
// → SELECT * FROM tst_users WHERE id IN (SELECT user_id FROM tst_orders WHERE status = ?)
Prefix in rawQuery()
rawQuery() automatically adds the prefix to table names that appear after
FROM, INTO, UPDATE, JOIN and DESCRIBE.
System tables like information_schema are never prefixed.
$db->setPrefix('tst_');
$db->rawQuery("SELECT * FROM users WHERE active = ?", [1]);
// → SELECT * FROM tst_users WHERE active = 1
$db->rawQuery("SELECT * FROM information_schema.COLUMNS WHERE ...");
// → SELECT * FROM information_schema.COLUMNS WHERE ...
// (system tables are never prefixed)
See also
- Standard Initialization – all connection parameters
- Multiple Instances – each instance can have its own prefix
- subQuery() – prefix behavior in subqueries explained