PHP code example of anax / database

1. Go to this page and download the library: Download anax/database 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 example snippets


/**
 * Config file for Database.
 *
 * Example for MySQL.
 *  "dsn" => "mysql:host=localhost;dbname=test;",
 *  "username" => "test",
 *  "password" => "test",
 *  "driver_options"  => [
 *      \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
 *  ],
 *
 * Example for SQLite.
 *  "dsn" => "sqlite::memory:",
 *  "dsn" => "sqlite:$path",
 *
 */
return [
    "dsn"             => null,
    "username"        => null,
    "password"        => null,
    "driver_options"  => null,
    "fetch_mode"      => \PDO::FETCH_OBJ,
    "table_prefix"    => null,
    "session_key"     => "Anax\Database",
    "emulate_prepares" => false,

    // True to be very verbose during development
    "verbose"         => null,

    // True to be verbose on connection failed
    "debug_connect"   => false,
];

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

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

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

                return $db;
            }
        ],
    ],
];

$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchAll($sql);

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

# $app style
$app->db->connect();

# $di style
$di->get("db")->connect();

$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchAll($sql);

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

$sql = "SELECT * FROM movie;";

# $app style
$app->db->connect();
$res = $app->db->execute($sql)->fetchAll();

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->execute($sql)->fetchAll();

$sql = "SELECT * FROM movie WHERE id = 1;";

# $app style
$app->db->connect();
$res = $app->db->executeFetch($sql);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetch($sql);

$sql = "SELECT * FROM movie WHERE id = ?;";

# $app style
$app->db->connect();
$res = $app->db->executeFetchClass($sql, [1], "\Anax\SomeClass");

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchClass($sql, [1], "\Anax\SomeClass");

$sql = "SELECT * FROM movie WHERE id = ?;";
$obj = new SomeClass();

# $app style
$app->db->connect();
$res = $app->db->executeFetchInto($sql, [1], $obj);

# $di style
$db = $di->get("db");
$db->connect();
$res = $db->executeFetchClass($sql, [1], $obj);

$sql = "UPDATE movie SET title = ? WHERE id = ?;";

# $app style
$app->db->connect();
$app->db->execute($sql, ["Some title", 1]);

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql, ["Some title", 1]);

$sql = "INSERT INTO movie (title) VALUES (?);";

# $app style
$app->db->connect();
$app->db->execute($sql, ["Some title"]);
$id = $app->lastInsertId();

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql, ["Some title"]);
$id = $db->lastInsertId();

$sql = "DELETE FROM movie;";

# $app style
$app->db->connect();
$app->db->execute($sql);
$num = $app->rowCount();

# $di style
$db = $di->get("db");
$db->connect();
$db->execute($sql);
$num = $db->rowCount();