LOSQL is a lightweight, object-oriented PHP database wrapper that simplifies database operations while preserving PDO's power. It offers developers a clean, efficient way to interact with MySQL, PostgreSQL, and other PDO-supported databases, ensuring maintainable code without performance trade-offs. Ideal for projects of any scale, LOSQL streamlines connection handling, query execution, and result processing while maintaining security and code organization.
<?php
require_once 'losql.php';
// Initialize database connection
$db = new LOSQL('db_user', 'db_pass', 'my_database', 'app_');
$db->connect();
// ===== SELECT Example =====
// Get all active users
$activeUsers = $db->query("SELECT * FROM #__users WHERE active = ?", 1)->fetchAll();
echo "Active users:\n";
print_r($activeUsers);
// Get a single user by ID
$user = $db->query("SELECT * FROM #__users WHERE id = ?", 42)->fetch();
echo "\nUser with ID 42:\n";
print_r($user);
// ===== INSERT Example =====
// Insert a new user
$newUserId = $db->lastId(); // Get the ID of the last inserted record
$db->query(
"INSERT INTO #__users (username, email, password, created_at) VALUES (?, ?, ?, NOW())",
'new_user',
'new@example.com',
password_hash('secret123', PASSWORD_DEFAULT)
);
$newUserId = $db->lastId();
echo "\nNew user created with ID: " . $newUserId;
// ===== UPDATE Example =====
// Update user's email
$rowsAffected = $db->query(
"UPDATE #__users SET email = ? WHERE id = ?",
'updated@example.com',
$newUserId
)->affectedRows();
echo "\nRows updated: " . $rowsAffected;
// ===== DELETE Example =====
// Delete inactive users older than 1 year
$oneYearAgo = date('Y-m-d H:i:s', strtotime('-1 year'));
$rowsDeleted = $db->query(
"DELETE FROM #__users WHERE active = ? AND created_at < ?",
0,
$oneYearAgo
)->affectedRows();
echo "\nInactive users deleted: " . $rowsDeleted;
// Close connection
$db->disconnect();