PHP Classes

How to Use a PHP SQL Query Builder to Generate and Execute SQL Queries at Run Time Using the PackageDyno SQL: Build and execute SQL queries dynamically

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-09-12 (29 days ago) RSS 2.0 feedNot enough user ratingsTotal: 49 All time: 10,716 This week: 21Up
Version License PHP version Categories
dynosql 1.0.0MIT/X Consortium ...7Databases, PHP 7
Description 

Author

This package can build and execute SQL queries dynamically.

It provides a class that can build a SQL query using functions of fluent interface to define query details like query type, tables, fields, parameters, etc...

It also provides a class to build SQL queries for a database table.

The package can connect to a given database using parameters passed using a configuration class object.

Picture of Cuthbert Martin Lwinga
  Performance   Level  
Name: Cuthbert Martin Lwinga <contact>
Classes: 5 packages by
Country: Canada Canada
Age: ???
All time rank: 405285 in Canada Canada
Week rank: 44 Up1 in Canada Canada Up
Innovation award
Innovation award
Nominee: 1x

Documentation

SQL Query Builder and Database Interaction Classes

This project provides a set of PHP classes for building SQL queries and interacting with a MySQL database. It offers a fluent interface for constructing queries, managing database connections, and performing CRUD operations on database tables.

Table of Contents

Features

  • Fluent interface for building SQL queries
  • Object-oriented representation of database tables and columns
  • Connection management with singleton pattern
  • CRUD operations (Create, Read, Update, Delete)
  • Support for joins, where clauses, ordering, and pagination
  • Automatic loading of table structure from the database

Classes

  1. QueryBuilder: Constructs SQL queries using a fluent interface.
  2. SqlColumn: Represents a column in a database table.
  3. SqlConnection: Manages database connections using a singleton pattern.
  4. SqlTable: Represents a database table and provides CRUD operations.
  5. DatabaseConfig: Contains database connection configuration.

Installation

  1. Clone the repository:
    git clone https://github.com/yourusername/your-repo-name.git
    
  2. Include the necessary files in your PHP project.

Usage

Building a Query

$query = (new QueryBuilder('users'))
    ->select(['id', 'name', 'email'])
    ->where('age', '>', 18)
    ->orderBy('name', 'ASC')
    ->limit(10);

$sql = $query->toSql();
$bindings = $query->getBindings();

Executing a Query

$connection = SqlConnection::getInstance();
$result = $query->execute($connection);

Working with Tables

$usersTable = new SqlTable('users');

// Insert
$userId = $usersTable->insert([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 30
]);

// Select
$users = $usersTable->select(['name', 'email'], ['age' => 30], 10, 0, ['name' => 'ASC']);

// Update
$usersTable->update(['age' => 31], ['id' => $userId]);

// Delete
$usersTable->delete(['id' => $userId]);

Configuration

Update the DatabaseConfig.php file with your database credentials:

abstract class DatabaseConfig
{
    const host = "your_host";
    const name = "your_database_name";
    const username = "your_username";
    const password = "your_password";
}


  Files folder image Files (7)  
File Role Description
Accessible without login Plain text file class_loader.php Example Example script
Accessible without login Plain text file DatabaseConfig.php Conf. Configuration class source
Plain text file QueryBuilder.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Plain text file SqlColumn.php Class Class source
Plain text file SqlConnection.php Class Class source
Plain text file SqlTable.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:49
This week:0
All time:10,716
This week:21Up