Skip to content

Core\Db::quoteIdentifier

Core\Db::quoteIdentifier(string $name): stringCore\Db::quoteIdentifier(
    string $name,
): string

Checks that a name is a plain SQL identifier, such as a table or column name, and returns it unchanged.

A value goes to the database as a placeholder, but a table or column name cannot. The name is written into the text of the statement, so Core\Db::quoteIdentifier checks it first. It accepts a name that starts with a letter or _ and continues with letters, digits or _. Every other name throws a LogicError. A few names, such as user and current_timestamp, also throw. The database reads them as values, such as its own user name.

Text from a request is tainted: Novis does not allow you to write it into a statement. The result of this method is not tainted, so you can use it.

The method checks the syntax only. A visitor who chooses the name can still pick any column, such as password_hash, and sort by it. Check a name a visitor chooses against a list of allowed names, as example 03 does.

Good to know: the result has no quotes or backticks around it. The method has no connection, so it does not know which database the name is for.

Parameters

  • $namestringThe identifier to check. It is returned exactly as given. Nothing is escaped, shortened or lower-cased, because a name that passes the check needs none of it.

Return value

Returns string. The same text, without the tainted qualifier. It has no quotes around it. This method has no connection, so it has no SQL dialect. The five databases use different quote characters.

Errors

  • LogicError$name is empty, starts with a digit, or contains a character that is not a letter, a digit or _. This includes a name that is valid only inside quotes. It is also thrown when the database reads $name as a value without quotes, such as current_user, user or current_timestamp, in upper or lower case.

Examples

Check a table name

<?nvs
// Shows how to check a table or column name before you write it into a
// statement. A name that is allowed comes back exactly as you gave it.
array<string> $names = ['orders', 'created_at', '_draft', 'Line2'];
// This prints the four names, one per line, unchanged.
foreach ($names as string $name) {
echo Core\Db::quoteIdentifier($name), "\n";
}
// A checked name is safe to write into the text of a statement. The value
// stays a placeholder.
string $table = Core\Db::quoteIdentifier('orders');
echo 'SELECT id, total FROM ', $table, ' WHERE total > ?', "\n";
Output
orders
created_at
_draft
Line2
SELECT id, total FROM orders WHERE total > ?

A name that is not allowed

<?nvs
// Shows which names are not allowed. A name with a space, a dot, a quote or a
// semicolon in it throws a `LogicError`. A name that starts with a digit
// throws too.
array<string> $names = [
'order items',
'public.orders',
'orders; DROP TABLE orders',
'2024_totals',
];
// This prints one "not an identifier" line for each of the four names.
foreach ($names as string $name) {
try {
echo Core\Db::quoteIdentifier($name), "\n";
} catch (LogicError $error) {
echo 'not an identifier: ', $name, "\n";
}
}
// A name with a schema in front of it is two names. Check each one, and write
// the dot between them yourself.
echo Core\Db::quoteIdentifier('public'), '.', Core\Db::quoteIdentifier('orders'), "\n";
Output
not an identifier: order items
not an identifier: public.orders
not an identifier: orders; DROP TABLE orders
not an identifier: 2024_totals
public.orders

Sort by a column the visitor chose

<?nvs
// Shows a list page that sorts by a column the visitor picked. The name must be
// one of the columns the page shows, and then it is checked before the
// statement is built.
class Report
{
public static function statement(string $sort, string $direction): string
{
// Only these columns may be used to sort. Any other name throws an error,
// even a real column such as `password_hash`.
array<bool> $allowed = ['created_at' => true, 'total' => true];
if (!Core\Arr::hasKey($allowed, $sort)) {
throw new LogicError('the page cannot sort by this column');
}
string $column = Core\Db::quoteIdentifier($sort);
string $order = 'ASC';
if ($direction == 'desc') {
$order = 'DESC';
}
return 'SELECT id, total FROM orders ORDER BY ' . $column . ' ' . $order . ' LIMIT ?';
}
}
// This prints the statement the database will run.
echo Report::statement('created_at', 'desc'), "\n";
// A visitor who sends something else gets no statement at all. The error comes
// from the check, before any text is built.
try {
echo Report::statement('total, (SELECT secret FROM users)', 'asc'), "\n";
} catch (LogicError $error) {
echo 'there is no column with that name', "\n";
}
Output
SELECT id, total FROM orders ORDER BY created_at DESC LIMIT ?
there is no column with that name