# Server client - PHP

> agdb example: managing users and databases and running queries on agdb_server from PHP

## What it shows

The PHP version of the server client example using the `agnesoft/agdb_api` package. It checks the server status, logs in as the admin, creates a user and a memory mapped database and runs all queries in a single batch, linking the inserted nodes with references to the results of earlier queries in the same batch (`:0`, `:1`).

## Run it

From the repository root:

```bash
pnpm run examples --filter agdb_api_php
```

The script installs the Composer dependencies, builds the `agdb_server` and runs the example against a freshly started server. The port 3000 must be free (stop any `agdb_server` you started yourself).

To use the client in your own project, add it with Composer:

```bash
composer require agnesoft/agdb_api:dev-main
```

See the [PHP client](/api-docs/php) page for details.

## Users and databases

```php
require "vendor/autoload.php";

use Agnesoft\AgdbApi\AgdbApi;
use Agnesoft\AgdbApi\CurlClient;
use Agnesoft\AgdbApi\DbKind;
use Agnesoft\AgdbApi\QueryBuilder;

// Create the client using the native cURL transport.
$client = AgdbApi::new(new CurlClient(), "localhost:3000");

// Runs the status query against the database
// and throws if the server is not accessible.
$client->status();

// Login as server admin.
$client->user_login("admin", "admin");

// Create user "php_user1".
$client->admin_user_add("php_user1", "php_user1");

// Login as "php_user1".
$client->user_login("php_user1", "php_user1");

// Creates memory mapped database "db1" for user "php_user1".
$client->db_add("php_user1", "db1", DbKind::Mapped);
```

## One batch with result references

Within a batch a query can refer to the result of an earlier query by its index prefixed with a colon. The edges are inserted from the result of query `:0` (the `users` root) to the result of query `:1` (the user nodes):

```php
$queries = [
    // :0: Inserts a root node aliased "users".
    QueryBuilder::insert()
        ->nodes()
        ->aliases(["users"])
        ->query(),

    // :1: Inserts more nodes with some data.
    QueryBuilder::insert()
        ->nodes()
        ->values([
            [
                "username" => "user1",
                "password" => "password123",
            ],
            [
                "username" => "user2",
                "password" => "password456",
            ],
        ])
        ->query(),

    // :2: Connect the root to the inserted nodes with edges referencing both from previous queries.
    QueryBuilder::insert()->edges()->from(":0")->to(":1")->query(),

    // :3: Find a node starting at the "users" node with specific username.
    QueryBuilder::select()
        ->search()
        ->from("users")
        ->where()
        ->key("username")
        ->value("user1")
        ->query(),
];

// Execute the queries.
$result = $client->db_exec_mut("php_user1", "db1", $queries);

// Print the result of the last query.
print_r($result[1][3]);
```

The client returns the HTTP status and the list of results, one per query, so `$result[1][3]` is the result of the search.

## Learn more

- [PHP client](/api-docs/php) - installing and using `agnesoft/agdb_api`.
- [Server reference](/docs/references/server)

<style>

html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sw1J6, html code.shiki .sw1J6{--shiki-light:#F76D47;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}

</style>

---

- [View source](https://github.com/agnesoft/agdb/tree/main/agdb_api/php/examples/server_client)
