Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple QueryBuilder PHP component

License

NotificationsYou must be signed in to change notification settings

co0lc0der/simple-query-builder-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest VersionGitHub repo sizePackagist DownloadsGitHub licensePackagist PHP Version Support

This is a small easy-to-use PHP component for working with a database by PDO. It provides some public methods to compose SQL queries and manipulate data. Each SQL query is prepared and safe. QueryBuilder fetches data toarrays by default. At present time the component supports MySQL and SQLite (file or memory).

PAY ATTENTION! v0.2 and v0.3+ are incompatible.

Contributing

Bug reports and/or pull requests are welcome

License

The package is available as open source under the terms of theMIT license

Installation

The preferred way to install this extension is throughcomposer.

Either run

composer require co0lc0der/simple-query-builder

or add

"co0lc0der/simple-query-builder":"*"

to therequire section of yourcomposer.json file.

How to use

Editconfig.php and set the parameters up. Choose DB driver, DB name etc

$config =require_once__DIR__ .'/config.php';

Use composer autoloader

require_once__DIR__ .'/vendor/autoload.php';useco0lc0der\QueryBuilder\Connection;useco0lc0der\QueryBuilder\QueryBuilder;

InitQueryBuilder withConnection::make()

$query =newQueryBuilder(Connection::make($config['database']));// $printErrors = false// for printing errors (since 0.3.6)$query =new QueryBuilder(Connection::make($config['database']),true)

Usage examples

Select all rows from a table

$results =$query->select('users')->all();

Result query

SELECT*FROM`users`;

Select rows with two conditions

$results =$query->select('users')->where([  ['id','>',1],'and',  ['group_id',2],])->all();

Result query

SELECT*FROM`users`WHERE (`id`>1)AND (`group_id`=2);

Update a row

$query->update('posts', ['status' =>'published'])        ->where([['YEAR(`updated_at`)','>',2020]])        ->go();

Result query

UPDATE`posts`SET`status`='published'WHERE (YEAR(`updated_at`)>2020);

More examples you can find indocumentation or tests.

ToDo

I'm going to add the next features into future versions

  • write more unit testes
  • add subqueries for QueryBuilder
  • addBETWEEN
  • addWHERE EXISTS
  • add TableBuilder class (for beginningCREATE TABLE, move$query->drop() and$query->truncate() into it)
  • add PostgreSQL support
  • addWITH
  • and probably something more

[8]ページ先頭

©2009-2025 Movatter.jp