{field}
Runs a SQL query on the application database and prints the single value it answers.
Syntax
{field "column from table where ..."}
{field "count(*) from orders", name='orderCount'}
Parameters
-
sql -
The query after the word SELECT -
"count(*) from users". The tag name is thedb()command word, so the parameter never starts with SELECT.
Options
-
name -
In the designer preview (
&padSample) the value of that name in the page's sample answers instead of the database;&padSample=capturerecords the database's answer under it.
Description
{field "..."} runs its parameter as a query on the application database and is replaced by the one value it answers - the first column of the first row. It is the template form of db ( "FIELD ..." ): the tag name itself becomes the command word of db(), which is why the parameter starts after the SELECT - {field "count(*) from orders"}, not {field "select count(*) ..."}.
The query runs on the database the application's $padSql* settings name (read from its .env), MySQL or SQLite. A query that finds no row answers an empty value. The parameter is SQL text as written: a value from the request does not belong in it. Fetch such a value in the page's PHP with a placeholder - db ( "FIELD name FROM users WHERE id={0}", [ $id ] ) - or write a named query in _data/name.sql, whose {$name} fields are bound as placeholders.
{field} shares its implementation with {record} and {array}; {check} is the yes-or-no variant. Do not confuse it with the field: type prefix, which reads a variable: {field:name}.
With name= the tag takes part in the designer preview: on ?page&padSample the page's _samples/page.json answers for it, so a designer can work on the template without a database.
Examples
A count
The single value a query answers, printed in place - no SELECT in the parameter.
<p>There are {field "count(*) from orders where status='open'"} open orders.</p>
With a sample name
name= lets the designer preview answer from _samples/ instead of the database.
<p>Customers: {field "count(*) from customers", name='customerCount'}</p>
See also
- {record} Runs a SQL query on the application database and renders its content with the first row's columns as fields.
- {array} Runs a SQL query on the application database and repeats its content once per row.
- {check} Asks the application database whether a row exists, and renders its content only when one does.