Skip to content

PD\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP.

License

Notifications You must be signed in to change notification settings

pardnchiu/PHP-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PD\SQL

PD\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP.

tag size
version download

Features

  • Fluent interface for building SQL queries
  • Safe parameter binding to prevent SQL injection
  • Support for complex JOIN operations (INNER, LEFT, RIGHT)
  • Dynamic WHERE clause construction
  • Ordering and pagination support
  • Transaction handling
  • Query execution time monitoring
  • Environment-based configuration
  • Automatic connection management

functions

  • Table selection with table()
  • Custom field selection with select()
  • Conditional filtering with where()
  • Join operations with join(), left_join(), right_join()
  • Result ordering with order_by()
  • Pagination with limit() and offset()
  • Record creation with insertGetId()
  • Record updates with update()
  • Total row count with total()
  • Raw query execution with query() for complex custom queries

How to Use

Install

composer require pardnchiu/sql
<?php

use PD\SQL;

$result_user_0 = SQL::table('users')
   ->where("status", "active")
   ->where("age", ">", 18)
   ->get();

$result_order = SQL::table("orders")
   ->select("orders.*", "users.name")
   ->join("users", "orders.user_id", "users.id")
   ->where("orders.status", "pending")
   ->get();

$result_product = SQL::table("products")
   ->total()
   ->limit(10)
   ->offset(0)
   ->order_by("created_at", "DESC")
   ->get();

$result_user_1 = SQL::query(
    "SELECT * FROM users WHERE status = ? AND role = ?",
    ["active", "admin"]
);

try {
    $result = SQL::table("users")
        ->where("id", 1)
        ->update([
            "status" => "active",
            "updated_at" => "NOW()"
        ]);

    if (!empty($result["info"])) {
        echo "Performance: " . $result["info"];
    };
} catch (\PDOException $e) {
    echo "Database Error: " . $e->getMessage();
} catch (\Exception $e) {
    echo "General Error: " . $e->getMessage();
};

License

This source code project is licensed under the MIT license.

Creator

邱敬幃 Pardn Chiu


©️ 2024 邱敬幃 Pardn Chiu

About

PD\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages