PHP code example of anax / database-query-builder

1. Go to this page and download the library: Download anax/database-query-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anax / database-query-builder example snippets


/**
 * Configuration file for database query builder service.
 */
return [
    // Services to add to the container.
    "services" => [
        "dbqb" => [
            "shared" => true,
            "callback" => function () {
                $obj = new \Anax\DatabaseQueryBuilder\DatabaseQueryBuilder();

                // Load the configuration files
                $cfg = $this->get("configuration");
                $config = $cfg->load("database");

                // Set the database configuration
                $connection = $config["config"] ?? [];
                $db->setOptions($connection);
                $db->setDefaultsFromConfiguration();

                return $db;
            }
        ],
    ],
];

$sql = "SELECT * FROM movie;";

$db = $di->get("dbqb");
$db->connect();
$res = $db->executeFetchAll($sql);

$this->db = new DatabaseQueryBuilder([
    "dsn" => "sqlite::memory:",
]);
$this->db->setDefaultsFromConfiguration();
$this->db->connect();

// Create a table
$this->db->createTable(
    'user',
    [
        'id'    => ['integer', 'primary key', 'not null'],
        'age'   => ['integer'],
        'name'  => ['varchar(10)']
    ]
)->execute();

$this->db->insert(
    "user",
    [
        "age" => 3,
        "name" => "three",
    ]
)->execute();

$last = $this->db->lastInsertId(); // 1
$rows = $this->db->rowCount();     // 1

$res = $this->db->select("*")
                ->from("user")
                ->where("id = 1")
                ->execute()
                ->fetch();

$res->id;   // 1
$res->age;  // 3
$res->name; // "three"