Basic queries

Basic queries to a database are done using DatabaseProvider::query. Since database providers are available as properties of the Database module, a simple query to a provider named main would look like this:

$result = $e->Database->main->query("select * from users");

Database queries are returned in the form of a DatabaseResult object you can manipulate. For example, use the DatabaseResult::isAny method to check if there were any results, or the DatabaseResult::countRows to get the number of rows in the result.

To loop through all the rows, iterate over the DatabaseResult::getRow method:

while ($row = $result->getRow()) {
    echo $row->getField("name")."\n";
}

Rows are returned as DatabaseRow objects which, as you can see in the example above, allow you to retrieve information from each row in the results. Here's the result:

Douglas Engelbart
John Horton Conway
Frank Abagnale
Carl Sagan
Richard Feynmann

See this example working in the Cherrycake documentation examples site.

Last updated