PHP code example of yousha / dbalite

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

    

yousha / dbalite example snippets


$config = [
    'host' => 'localhost',
    'dbname' => 'test_database',
    'user' => 'test_username',
    'password' => 'test_password',
    'ssl_ca' => '/path/to/ca-cert.pem', // Optional: Path to SSL CA certificate
    'ssl_verify' => false,             // Optional: Disable server cert verification
];

$config = [
    'driver' => OracleDriver::class,
    'database' => [
        'host' => 'localhost',
        'port' => 1521,
        'dbname' => 'test_database',
        'user' => 'test_username',
        'password' => 'test_password',
    ],
];

$config = [
    'driver' => SQLiteDriver::class,
    'database' => [
        'path' => __DIR__ . '/testdatabase.sqlite',
    ],
];

use Yousha\DBALite\Dbal;
use Yousha\DBALite\ConfigManager;

$config = new ConfigManager([
    'database' => $config,
    'migrations_dir' => __DIR__ . '/migrations', // Optional: Path to migration files.
]);

$dbal = new Dbal($config);

// Get query builder
$queryBuilder = $dbal->getQueryBuilder();

// Build and execute a query
$sql = $queryBuilder
    ->from('users')
    ->where('status', 'active')
    ->limit(10)
    ->getSQL();

$params = $queryBuilder->getParams();
$results = $dbal->getDriver()->query($sql, $params);

print_r($results);

// Insert a new user
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$params = ['name' => 'John Doe', 'email' => '[email protected]'];
$affectedRows = $dbal->getDriver()->execute($sql, $params);

echo "Inserted rows: $affectedRows\n";

try {
    $dbal->getTransactionHandler()->beginTransaction();

    // Perform multiple operations
    $dbal->getDriver()->execute("INSERT INTO users (name, email) VALUES (:name, :email)", [
        'name' => 'Jane Doe',
        'email' => '[email protected]',
    ]);

    $dbal->getDriver()->execute("UPDATE users SET status = :status WHERE id = :id", [
        'status' => 'inactive',
        'id' => 1,
    ]);

    $dbal->getTransactionHandler()->commitTransaction();
    echo "Transaction committed successfully.\n";
} catch (\Exception $e) {
    $dbal->getTransactionHandler()->rollbackTransaction();
    echo "Transaction failed: " . $e->getMessage() . "\n";
}

$schemaManager = $dbal->getDriver()->getSchemaManager();

// Create a table
$schemaManager->createTable('products', [
    'id' => 'INT AUTO_INCREMENT PRIMARY KEY',
    'name' => 'VARCHAR(255) NOT NULL',
    'price' => 'DECIMAL(10, 2) NOT NULL',
]);

// Add a column
$schemaManager->addColumn('products', 'description', 'TEXT');

// Check if a column exists
if ($schemaManager->columnExists('products', 'description')) {
    echo "Column 'description' exists!\n";
}

use Yousha\DBALite\Security\Sanitizer;

$input = "<script>alert('XSS');</script>";
$sanitizedInput = Sanitizer::sanitizeInput($input);

echo $sanitizedInput; // Output: &lt;script&gt;alert(&#039;XSS&#039;);&lt;/script&gt;

$config->merge([
    'database' => [
        'ssl_ca' => '/path/to/new-ca-cert.pem',
    ],
]);

echo $config->get('database.ssl_ca'); // Output: /path/to/new-ca-cert.pem
shell
php bin/dbalite-migrate.php --action=up