PAD talks to the application's database - configured in
_config/config.php - in two places: a page's PHP half calls
db(), and the template side has tags that ask directly. The examples here read
the staff table of the demo database - four people of its own, not the five of
the staff.xml file the manual's other examples use.
The first word of the statement says what shape comes back:
| word | answers with |
|---|---|
field | one value |
record | one row, as an array of fields |
array | all rows, ready for a tag to iterate |
check | whether a row exists - written check table where ..., with no column list |
insert / update | writes, taking their values from an array |
|
PHP fragments/database_1.php |
HTML fragments/database_1.pad |
Result |
|---|---|---|
|
|
Jim's phone number is 555-4364 |
An array lands in a variable, and the variable's name is a tag - with the
else construct for when the query found nothing:
|
PHP fragments/database_2.php |
HTML fragments/database_2.pad |
Result | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
|
The same questions can be asked where the answer is used: a {field}
tag drops one value into the page, and an {array} pair iterates its
query on the spot. Inside the quotes a comma is part of the query, so a column list is
written as it is - {array "name, phone from staff"} - and the example
keeps to *:
|
PHP |
HTML fragments/database_3.pad |
Result | ||||||
|---|---|---|---|---|---|---|---|---|
|
Bob's phone number is 555-3425
|
A {record} pair does the same for a single row.
A query can also live in a file of its own. _data/staffByPhone.sql holds:
-- The staff whose phone number matches a pattern, at most so many of them.
select name, phone
from staff
where phone like {$pattern}
order by name
limit {$max}
and the tag {staffByPhone} runs it. The file is never run as PAD:
each {$name} is bound as a placeholder - an option of the tag first,
then a variable of the page - so a value can never become SQL. The statement must read.
|
PHP fragments/database_4.php |
HTML fragments/database_4.pad |
Result |
|---|---|---|
|
|
bob jerry jim joe bob jerry |
For an application built around its tables there is a third way: declare the tables once
in the configuration and every one of them is a tag, relations and automatic joins
included - the select: prefix. The
classicModels application is that subsystem
demonstrated over a whole database, and docs/DATABASE.md in the repository is
the reference.