PHP code example of patryknamyslak / patbase

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

    

patryknamyslak / patbase example snippets


use PatrykNamyslak\Patbase\Facades\DB;
use PatrykNamyslak\Patbase\Enums\WhereOperator;

// SELECT * FROM `blog` WHERE views < :views;
$selectQuery = DB::select([*])->from("blog")->where("views", WhereOperator::LESS_THAN, 500)->all()
// The all() method runs the query by automatically swapping in the parameters in for the value you set in the where part of the chain!


DB::insert()->into("table")->set("title", "New Post title");
DB::upsert(["title"])->into("table")->...;
// OR
DB::insertOrUpdate()
DB::delete()->from("blog")->where("title", WhereOperator::LIKE, "Flag API");

DB::update()->table("table")->...;


use PatrykNamyslak\Patbase\Patbase;
use PatrykNamyslak\Patbase\Enums\DatabaseDriver;

$db = new Patbase(
    driver: DatabaseDriver::MYSQL,
    database: 'myapp',
    username: 'root',
    password: 'Password123@',
    host: 'localhost',
    port: NULL, // NULL (or omit as the default is NULL) or your custom port, NULL will default to the port based on driver type i.e MYSQL = 3306
);

$users = $db->query("SELECT * FROM `users`;")->fetchAll();

$db = new Patbase(
    driver: DatabaseDriver::POSTGRES,
    database: 'myapp',
    username: 'postgres',
    password: 'secret',
    host: 'localhost'
    // port: 5432 (automatic)
);

$db = new Patbase(
    driver: DatabaseDriver::SQL_LITE,
    database: './database.db'
    // No username/password needed
);

$user = $db->prepare(
    "SELECT * FROM users WHERE email = :email",
    [':email' => '[email protected]']
)->fetch();

// Don't connect yet
$db = new Patbase(
    driver: DatabaseDriver::MYSQL,
    database: 'myapp',
    username: 'root',
    password: 'secret',
    autoConnect: false
);

// Optionally modify DSN
$db->dsn('mysql:host=127.0.0.1;port=3307;dbname=myapp');

// Connect when ready (or auto-connects on first query)
$db->connect();

$db = new Patbase(
    driver: DatabaseDriver::MYSQL,
    database: 'myapp',
    username: 'root',
    password: 'secret',
    options: [
        PDO::ATTR_PERSISTENT => true,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
    ]
);

if (!$db->isConnected()) {
    $db->connect();
}

$pdo = $db->connection();
// Use native PDO methods if needed