Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/database-connection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Basic Auth Example

This example demonstrates how to Connect Database in Dumbo.

## Running the Example

1. Install dependencies:

```bash
composer install
```

2. Start the server:

```bash
composer start
```

3. Access the protected route:

```bash
curl -u user:password http://localhost:8000
```
18 changes: 18 additions & 0 deletions examples/database-connection/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"require": {
"notrab/dumbo": "@dev"
},
"repositories": [
{
"type": "path",
"url": "../../"
}
],
"scripts": {
"start": [
"Composer\\Config::disableProcessTimeout",
"php -S localhost:8000 -t ."
]
}
}

29 changes: 29 additions & 0 deletions examples/database-connection/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require __DIR__ . '/vendor/autoload.php';

use Dumbo\Dumbo;
use Dumbo\Helpers\DatabaseHelper;


$app = new Dumbo();

$app->use(function ($container) {
$dbHelper = new DatabaseHelper('localhost', 'your_db', 'username', 'password');
$container->set('db', $dbHelper);
});

$app->get('/', function ($container) {
$db = $container->get('db');
$queryBuilder = $db->table('your_table');

try {
$results = $queryBuilder->select('*')->get();
foreach ($results as $result) {
print_r($result);
}
} catch (Exception $e) {
error_log('Database error: ' . $e->getMessage());
}
});
$app->run();
103 changes: 103 additions & 0 deletions src/Helpers/DatabaseHelpler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Dumbo\Helpers;

use PDO;
use PDOException;
use Exception;

class DatabaseHelper
{
private $pdo;

public function __construct($host, $dbname, $user, $password)
{
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8";
try {
$this->pdo = new PDO($dsn, $user, $password);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
error_log('Database connection error: ' . $e->getMessage());
throw new Exception('Database connection error');
}
}

public function table($table)
{
return new QueryBuilder($this->pdo, $table);
}
}

class QueryBuilder
{
private $pdo;
private $table;
private $select = '*';
private $where = [];
private $params = [];

public function __construct(PDO $pdo, $table)
{
$this->pdo = $pdo;
$this->table = $table;
}

public function select($columns = '*')
{
$this->select = $columns;
return $this;
}

public function where($column, $operator, $value)
{
$this->where[] = "$column $operator :$column";
$this->params[$column] = $value;
return $this;
}

public function get()
{
$whereClause = count($this->where) ? 'WHERE ' . implode(' AND ', $this->where) : '';
$sql = "SELECT {$this->select} FROM {$this->table} $whereClause";
$stmt = $this->pdo->prepare($sql);
$stmt->execute($this->params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function insert($data)
{
$fields = implode(", ", array_keys($data));
$placeholders = ":" . implode(", :", array_keys($data));
$sql = "INSERT INTO {$this->table} ($fields) VALUES ($placeholders)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute($data);
return $this->pdo->lastInsertId();
}

public function update($data)
{
$set = "";
foreach ($data as $key => $value) {
$set .= "$key = :$key, ";
}
$set = rtrim($set, ", ");
$sql = "UPDATE {$this->table} SET $set";
if ($this->where) {
$sql .= ' WHERE ' . implode(' AND ', $this->where);
}
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array_merge($data, $this->params));
return $this;
}

public function delete()
{
$sql = "DELETE FROM {$this->table}";
if ($this->where) {
$sql .= ' WHERE ' . implode(' AND ', $this->where);
}
$stmt = $this->pdo->prepare($sql);
$stmt->execute($this->params);
return $this;
}
}