> For the complete documentation index, see [llms.txt](https://sprnva.gitbook.io/sprnva-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sprnva.gitbook.io/sprnva-docs/database/query.md).

# Query

This is for making a raw query against the database. The `$query` is the statement you need to execute. The `$fetch` parameter is optional only, if you want to retrieve the result of the statement just change the `$fetch = "Y"`.

If `$fetch = "Y"` then add the `->get();` mehod to get the result otherwise do not put `->get();`.

```php
// DB()->query($query, $fetch = "N");

<?php

namespace App\Controllers;

class UserController
{
    public function index()
    {
        $test_query = DB()->query('SELECT * FROM users WHERE id > 0', 'Y')->get();
        
        return view('/index', compact('test_query'));
    }
}
```

And this is how we retrieve the result of the query:

```php
// in your views
foreach($test_query as $test){
    echo $test['fullname'];
}
```

Another example is using the `query` method to update a column data in a table.

```php
<?php

namespace App\Controllers;

class UserController
{
    public function update()
    {
        DB()->query('UPDATE users SET fullname = 'test name only' WHERE id = 1');
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://sprnva.gitbook.io/sprnva-docs/database/query.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.
