PHP code example of fastpress / database

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

    

fastpress / database example snippets




use Fastpress\Memory\Database;

$config = [
    'host' => 'localhost',
    'port' => '3306',
    'database' => 'your_database',
    'charset' => 'utf8mb4',
    'username' => 'your_username',
    'password' => 'your_password'
];

$db = new Database($config);

// Select a single row
$user = $db->select('users', ['id', 'name', 'email'], ['id' => 1]);
if ($user) {
    echo "Name: {$user['name']}, Email: {$user['email']}";
}

// Select multiple rows
$allUsers = $db->selectAll('users', ['id', 'name', 'email']);
foreach ($allUsers as $u) {
    echo "ID: {$u['id']}, Name: {$u['name']}\n";
}

// Insert a new row
$newUserId = $db->insert('users', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);
echo "Inserted user ID: $newUserId";

// Update existing rows
$rowsUpdated = $db->update('users', [
    'email' => '[email protected]'
], [
    'id' => $newUserId
]);
echo "$rowsUpdated row(s) updated.";

// Delete rows
$rowsDeleted = $db->delete('users', [
    'id' => $newUserId
]);
echo "$rowsDeleted row(s) deleted.";

// Running a raw query
$stmt = $db->query("SELECT COUNT(*) as total FROM `users`");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo "Total users: {$result['total']}";

select($table, $columns = ['*'], $where = [])
// Fetches a single row.

selectAll($table, $columns = ['*'], $where = [])
// Fetches all rows matching criteria.

insert($table, $data)
// Inserts a new row and returns the inserted ID.

update($table, $data, $where)
// Updates rows matching where conditions and returns the number of affected rows.

delete($table, $where)
// Deletes rows matching where conditions and returns the number of affected rows.

query($sql, $params = [])
// Executes a raw SQL query.

getQueries()
// Returns an array of executed queries.