Skip to content

Instantly share code, notes, and snippets.

@mhmtsnmzkanly
Created March 5, 2025 07:29
Show Gist options
  • Select an option

  • Save mhmtsnmzkanly/9f104c764fda8135fbd7d267af6d3193 to your computer and use it in GitHub Desktop.

Select an option

Save mhmtsnmzkanly/9f104c764fda8135fbd7d267af6d3193 to your computer and use it in GitHub Desktop.
<?php
if (!Defined("ROYAL"))
exit();
class Royal
{
private PDO $connection;
public function __construct(array $config)
{
try {
$this->connection = new PDO('mysql:host=' . $config["db"]["host"] . ';dbname=' . $config["db"]["db"], $config["db"]["username"], $config["db"]["password"]);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
print_r($e);
exit;
}
}
public function db(): PDO
{
return $this->connection;
}
public function Query(string $query)
{
return $this->connection->query($query);
}
public function Prepare(string $query)
{
return $this->connection->prepare($query);
}
public function Execute(string $query, array $params = [])
{
try {
$statement = $this->Prepare($query);
$statement->execute($params);
return $statement;
} catch (PDOException $e) {
print_r($e);
exit;
}
}
}
$Royal = new Royal([
"db" => [
"host" => "localhost",
"username" => "root",
"password" => "",
"db" => "royal"
]
]);
$LinkList = $Royal->Execute("SELECT * FROM links WHERE is_active = '1'")->fetchAll(PDO::FETCH_OBJ);
$getUserQuery = $Royal->Prepare("SELECT * FROM users WHERE username = :un");
$getUserQuery->bindParam(":un", $un, PDO::PARAM_STR);
$getUserQuery->execute();
$user = $getUserQuery->fetch(PDO::FETCH_OBJ);
@mhmtsnmzkanly
Copy link
Author

mhmtsnmzkanly commented Dec 14, 2025

Yapay zeka düzeltti:

<?php
declare(strict_types=1);

defined('ROYAL') or exit();

final class Royal
{
    private PDO $db;

    public function __construct(array $config)
    {
        $dsn = sprintf(
            'mysql:host=%s;dbname=%s;charset=utf8mb4',
            $config['db']['host'],
            $config['db']['db']
        );

        $this->db = new PDO(
            $dsn,
            $config['db']['username'],
            $config['db']['password'],
            [
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                PDO::ATTR_EMULATE_PREPARES   => false,
            ]
        );
    }

    public function query(string $sql, array $params = []): PDOStatement
    {
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }

    public function fetchAll(string $sql, array $params = []): array
    {
        return $this->query($sql, $params)->fetchAll();
    }

    public function fetch(string $sql, array $params = []): object|null
    {
        $result = $this->query($sql, $params)->fetch();
        return $result === false ? null : $result;
    }

    public function pdo(): PDO
    {
        return $this->db;
    }
}

/* ---------- KULLANIM ---------- */

$royal = new Royal([
    'db' => [
        'host'     => 'localhost',
        'username' => 'root',
        'password' => '',
        'db'       => 'royal',
    ]
]);

$links = $royal->fetchAll(
    "SELECT * FROM links WHERE is_active = :active",
    ['active' => 1]
);

$user = $royal->fetch(
    "SELECT * FROM users WHERE username = :username",
    ['username' => $un]
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment