PHP code example of commandstring / pdo

1. Go to this page and download the library: Download commandstring/pdo 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/ */

    

commandstring / pdo example snippets


$driver = (new Driver)
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->withPort(3306)
	->withPrefix(Driver::PREFIX_MYSQL)
	->connect()
;

$driver = Driver::createMySqlDriver("root", "password", "database")->connect();
$driver = Driver::createPostgresSqlDriver("root", "password", "database")->connect();

// you can set the host and port in the last two parameters but they default to localhost and the default port of the service

$rows = $driver->query("SELECT * FROM table")->fetchAll(PDO::FETCH_ASSOC);

$driver->prepare("SELECT * FROM table WHERE column = :column");
$driver->bindValue("column", "value");
$driver->execute();
$rows = $driver->fetchAll(PDO::FETCH_ASSOC);

(new Driver(true))
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->connect()
;

function getRowWhereIdIs(int $id, int $fetch_type = PDO::FETCH_ASSOC): mixed
{
	Driver::prepare("SELECT * FROM table WHERE id = :id");
	Driver::bindValue("id", $id);
	Driver::execute();
	return Driver::fetch($fetch_type);
}

$row = getRowWhereIdIs(20);


$driver->select()
    ->from("table")
    ->columns(["column" => "column_alias_name"], "column2")
    ->orderBy("column", "ASC")
    ->limit(20)
    ->offset(30)
;

$driver->insert()
    ->into("table")
    ->value("column", "value")
    ->values(["column2" => "value", "column3" => "value"])
;

$driver->update()
    ->table("table")
    ->set("column", "newValue")
    ->where("column", "=", "value")
;

$driver->delete()
    ->from("table")
    ->where("column", "=", "value")

// ...
->where("column", "=", "value")
->where("column", "IN", [1, 5, "hi"])
->where("column", "IN", [(new Select($driver))->from("table")->columns("column")])
->where("column", "BETWEEN", [0, 5])
->whereOr("column", "=", 5)
->whereNot("column", "=", 10)

$storableStmt = $driver->storableStatements->create("accounts.getByUsername")

$storableStmt->setStatement($driver->select()->from("accounts")->columns("name", "id"));

$storableStmt->setBeforeHandler(function (Select $statement, string $name): Select
{
    return $statement->where("name", "=", $name);
});

$storableStmt->setAfterHandler(function (PDOStatement $statement)): Account
{
    $results = $statement->fetch(PDO::FETCH_OBJ);

    return new Account($results->name, $results->id);
}

$driver->storableStatements->execute("accounts.getByUsername", ["Command_String"]);