メインコンテンツまでスキップ
Version: Fegg 5.x(開発版)

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.

  • $parameter is 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 = 1 yourself.
  • Only queries that start with SELECT or PRAGMA return rows (all, one) and run on a slave. Any other query (including WITH ... SELECT) runs on the master as a statement; read getAffectedRow after it.

Arguments​

NameData typeDefaultRemarks
$querystringSQL with ? placeholders
$parameterarray[]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();