Pdo::setQuery
Set query string directly
Description
public self Pdo::setQuery( string $query, [ array $parameter = [] ] )
Sets the SQL to run with execute, as it is.
$parameteris the list of values for the?placeholders, in order. Arrays are not expanded here (they are in setWhere); write one?per value.- Nothing is added automatically: write
valid = 1yourself. - Only queries that start with
SELECTorPRAGMAreturn rows (all, one) and run on a slave. Any other query (includingWITH ... SELECT) runs on the master as a statement; read getAffectedRow after it.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $query | string | SQL with ? placeholders | |
| $parameter | array | [] | Values, in placeholder order |
Example
Controller
$db = new \Fegg\Database\Pdo();
$rows = $db->setQuery(
'SELECT category, count(*) AS n FROM cms_news WHERE valid = 1 GROUP BY category HAVING n > ?',
array(1)
)->execute()->all();
$db->setQuery('UPDATE cms_news SET visible = ? WHERE post_date < ?', array(0, '2020-01-01'))->execute();
$updated = $db->getAffectedRow();