# Basic queries

Basic queries to a database are done using [DatabaseProvider::query](/version-0.x/reference/core-classes/databaseprovider/databaseprovider-methods.md#query). Since database providers are available as properties of the Database module, a simple query to a provider named `main` would look like this:

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

Database queries are returned in the form of a [DatabaseResult](/version-0.x/reference/core-classes/databaseresult.md) object you can manipulate. For example, use the [DatabaseResult::isAny](/version-0.x/reference/core-classes/databaseresult/databaseresult-methods.md#isany) method to check if there were any results, or the [DatabaseResult::countRows](/version-0.x/reference/core-classes/databaseresult/databaseresult-methods.md#countrows) to get the number of rows in the result.

To loop through all the rows, iterate over the [DatabaseResult::getRow](/version-0.x/reference/core-classes/databaseresult/databaseresult-methods.md#getrow) method:

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

Rows are returned as [DatabaseRow](/version-0.x/reference/core-classes/databaserow.md) 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
```

{% hint style="success" %}
See this example working in the [Cherrycake documentation examples ](https://documentation-examples.cherrycake.io/example/databaseGuideBasicQueries)site.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cherrycake.tin.cat/version-0.x/guide/database-guide/basic-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
