Skip to content

Instantly share code, notes, and snippets.

@shiv122
Last active April 30, 2024 06:24
Show Gist options
  • Save shiv122/ed100eb0e1e47543b6e5f01a12a433c7 to your computer and use it in GitHub Desktop.
Save shiv122/ed100eb0e1e47543b6e5f01a12a433c7 to your computer and use it in GitHub Desktop.
<?php
// Database configuration
$dbHost = 'localhost'; // Change this to your database host
$dbName = 'your_database_name'; // Change this to your database name
$dbUser = 'your_username'; // Change this to your database username
$dbPass = 'your_password'; // Change this to your database password
// PDO connection
try {
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
?>
<?php
require 'dbconfig.php';
class QueryBuilder {
protected $pdo;
protected $table;
protected $where = [];
protected $bindings = [];
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
public function table($table) {
$this->table = $table;
return $this;
}
public function where($column, $operator, $value) {
$this->where[] = "$column $operator ?";
$this->bindings[] = $value;
return $this;
}
public function select($columns = '*') {
$sql = "SELECT $columns FROM $this->table";
if (!empty($this->where)) {
$sql .= " WHERE " . implode(' AND ', $this->where);
}
$statement = $this->pdo->prepare($sql);
$statement->execute($this->bindings);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
public function insert($data) {
$columns = implode(', ', array_keys($data));
$values = implode(', ', array_fill(0, count($data), '?'));
$sql = "INSERT INTO $this->table ($columns) VALUES ($values)";
$statement = $this->pdo->prepare($sql);
$statement->execute(array_values($data));
return $this->pdo->lastInsertId();
}
public function update($data) {
$set = [];
foreach ($data as $key => $value) {
$set[] = "$key = ?";
$this->bindings[] = $value;
}
$set = implode(', ', $set);
$sql = "UPDATE $this->table SET $set WHERE " . implode(' AND ', $this->where);
$statement = $this->pdo->prepare($sql);
$statement->execute($this->bindings);
return $statement->rowCount();
}
public function delete() {
$sql = "DELETE FROM $this->table WHERE " . implode(' AND ', $this->where);
$statement = $this->pdo->prepare($sql);
$statement->execute($this->bindings);
return $statement->rowCount();
}
public function rawQuery($sql, $bindings = []) {
$statement = $this->pdo->prepare($sql);
$statement->execute($bindings);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
}
// Usage example:
$queryBuilder = new QueryBuilder($pdo);
// Method chaining example
$users = $queryBuilder->table('users')->where('age', '>', 18)->select();
print_r($users);
// Insert example
$newUserId = $queryBuilder->table('users')->insert(['name' => 'Jane Doe', 'age' => 25, 'email' => 'jane@example.com']);
echo "New user ID: $newUserId";
// Update example
$updatedRows = $queryBuilder->table('users')->where('id', '=', 1)->update(['age' => 30, 'name' => 'Updated Name']);
echo "Updated rows: $updatedRows";
// Delete example
$deletedRows = $queryBuilder->table('users')->where('id', '=', 2)->delete();
echo "Deleted rows: $deletedRows";
// Raw query example
$customQuery = "SELECT * FROM users WHERE age > ?";
$results = $queryBuilder->rawQuery($customQuery, [20]);
print_r($results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment