Database best practices

Best practices (all versions)

Avoid methods that do automatic html conversion, like query_result and fetchByAssoc(...)

Never concatenate variables (especially coming from request) into the sql string

Always use the no_html versions (query_result_no_html or fetchByAssoc(..., -1, false)) or the new shortcut methods (see below)

Always use prepared statement (pquery), to avoid SQL injections

In SELECT statements, select only the necessary columns, avoid * if you don't need all the columns

In INSERT statements, always specify the column names (to avoid breaking the query in case of new columns added later)

Shortcut methods (version ≥ 26.01)

New utility methods have been added to easy query execution and retrieval of rows. None of these functions will do any html conversion of the result.

In version 26.01, there is still a main source of queries transforming data to html: reading records. So all queries reading records to be displayed in ListView, DetailView, EditView, Reports... are still converting to html.
This behaviour was kept to avoid too many breaks with existing custom code (uitypes, views, presave, ...)

Aliased methods (vtenext ≥ 26.01)

Some methods in PearDatabase have now new aliases, to better remember that a html conversion can take place:

It is recommended not to use the html methods, but if really necessary, try to use the "html" aliases.

New methods (version ≥ 26.04)


Revision #17
Created 2026-01-26 13:10:20 UTC by manuel.tagliapietra
Updated 2026-04-20 14:42:02 UTC by manuel.tagliapietra