1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 02:43:09 +01:00
php-dibi/readme.md

140 lines
4.1 KiB
Markdown
Raw Normal View History

2015-12-14 14:25:29 +01:00
[Dibi](https://dibiphp.com) - smart database layer for PHP [![Buy me a coffee](https://files.nette.org/images/coffee1s.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9XXL5ZJHAYQUN)
2013-06-23 01:47:17 +02:00
=========================================================
2014-05-13 15:56:44 +02:00
[![Downloads this Month](https://img.shields.io/packagist/dm/dibi/dibi.svg)](https://packagist.org/packages/dibi/dibi)
[![Build Status](https://travis-ci.org/dg/dibi.svg?branch=master)](https://travis-ci.org/dg/dibi)
2015-11-04 03:57:52 +01:00
[![Build Status Windows](https://ci.appveyor.com/api/projects/status/github/dg/dibi?branch=master&svg=true)](https://ci.appveyor.com/project/dg/dibi/branch/master)
2015-06-15 15:05:45 +02:00
[![Latest Stable Version](https://poser.pugx.org/dibi/dibi/v/stable)](https://github.com/dg/dibi/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/dg/dibi/blob/master/license.md)
2014-05-13 15:56:44 +02:00
2017-09-26 16:04:35 +02:00
Introduction
------------
2013-06-23 01:47:17 +02:00
Database access functions in PHP are not standardised. This library
2013-12-27 11:33:16 +01:00
hides the differences between them, and above all, it gives you a very handy interface.
2013-06-23 01:47:17 +02:00
2017-09-26 16:04:35 +02:00
Installation
2017-09-26 16:04:35 +02:00
------------
2013-06-23 01:47:17 +02:00
2017-09-26 16:04:35 +02:00
The recommended way to install Dibi is via Composer (alternatively you can [download package](https://github.com/dg/dibi/releases)):
2013-06-23 01:47:17 +02:00
```bash
composer require dibi/dibi
```
2013-06-23 01:47:17 +02:00
2017-09-26 16:04:35 +02:00
The Dibi 3.x requires PHP version 5.4.4 and supports PHP up to 7.2, version 4 requires PHP 7.1.
2013-07-01 12:37:28 +02:00
2017-09-26 16:04:35 +02:00
Usage
-----
2013-07-01 12:37:28 +02:00
2013-06-23 01:47:17 +02:00
Refer to the `examples` directory for examples. Dibi documentation is
2015-12-14 14:25:29 +01:00
available on the [homepage](https://dibiphp.com).
2013-06-23 01:47:17 +02:00
2013-07-01 12:37:28 +02:00
Connect to database:
```php
2018-03-09 14:53:28 +01:00
$dibi = new Dibi\Connection([
'driver' => 'mysqli',
2013-07-01 12:37:28 +02:00
'host' => 'localhost',
'username' => 'root',
'password' => '***',
2015-10-06 01:39:01 +02:00
]);
2013-07-01 12:37:28 +02:00
2018-03-09 14:53:28 +01:00
// or static way; in all other examples use dibi:: instead of $dibi->
dibi::connect($options);
2013-07-01 12:37:28 +02:00
```
SELECT, INSERT, UPDATE
```php
2018-03-09 14:53:28 +01:00
$dibi->query('SELECT * FROM users WHERE id = ?', $id);
2013-07-01 12:37:28 +02:00
2015-10-06 01:39:01 +02:00
$arr = [
2013-07-01 12:37:28 +02:00
'name' => 'John',
'is_admin' => true,
2015-10-06 01:39:01 +02:00
];
2018-03-09 14:53:28 +01:00
$dibi->query('INSERT INTO users', $arr);
2013-07-01 12:37:28 +02:00
// INSERT INTO users (`name`, `is_admin`) VALUES ('John', 1)
2018-03-09 14:53:28 +01:00
$dibi->query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
2013-07-01 12:37:28 +02:00
// UPDATE users SET `name`='John', `is_admin`=1 WHERE `id` = 123
2018-03-09 14:53:28 +01:00
$dibi->query('UPDATE users SET', [
2013-07-01 12:37:28 +02:00
'title' => array('SHA1(?)', 'tajneheslo'),
2015-10-06 01:39:01 +02:00
]);
2013-07-01 12:37:28 +02:00
// UPDATE users SET 'title' = SHA1('tajneheslo')
```
Getting results
```php
2018-03-09 14:53:28 +01:00
$result = $dibi->query('SELECT * FROM users');
2013-07-01 12:37:28 +02:00
$value = $result->fetchSingle(); // single value
$all = $result->fetchAll(); // all rows
$assoc = $result->fetchAssoc('id'); // all rows as associative array
$pairs = $result->fetchPairs('customerID', 'name'); // all rows as key => value pairs
// iterating
foreach ($result as $n => $row) {
print_r($row);
}
```
Modifiers for arrays:
```php
2018-03-09 14:53:28 +01:00
$dibi->query('SELECT * FROM users WHERE %and', [
2013-07-01 12:37:28 +02:00
array('number > ?', 10),
array('number < ?', 100),
2015-10-06 01:39:01 +02:00
]);
2013-07-01 12:37:28 +02:00
// SELECT * FROM users WHERE (number > 10) AND (number < 100)
```
<table>
<tr><td> %and </td><td> </td><td> `[key]=val AND [key2]="val2" AND ...` </td></tr>
<tr><td> %or </td><td> </td><td> `[key]=val OR [key2]="val2" OR ...` </td></tr>
<tr><td> %a </td><td> assoc </td><td> `[key]=val, [key2]="val2", ...` </td></tr>
<tr><td> %l %in </td><td> list </td><td> `(val, "val2", ...)` </td></tr>
<tr><td> %v </td><td> values </td><td> `([key], [key2], ...) VALUES (val, "val2", ...)` </td></tr>
<tr><td> %m </td><td> multivalues </td><td> `([key], [key2], ...) VALUES (val, "val2", ...), (val, "val2", ...), ...` </td></tr>
<tr><td> %by </td><td> ordering </td><td> `[key] ASC, [key2] DESC ...` </td></tr>
<tr><td> %n </td><td> identifiers </td><td> `[key], [key2] AS alias, ...` </td></tr>
<tr><td> other </td><td> - </td><td> `val, val2, ...` </td></tr>
</table>
Modifiers for LIKE
```php
2018-03-09 14:53:28 +01:00
$dibi->query("SELECT * FROM table WHERE name LIKE %like~", $query);
2013-07-01 12:37:28 +02:00
```
<table>
<tr><td> %like~ </td><td> begins with </td></tr>
<tr><td> %~like </td><td> ends with </td></tr>
<tr><td> %~like~ </td><td> contains </td></tr>
</table>
DateTime:
```php
2018-03-09 14:53:28 +01:00
$dibi->query('UPDATE users SET', [
2013-07-01 12:37:28 +02:00
'time' => new DateTime,
2015-10-06 01:39:01 +02:00
]);
2013-07-01 12:37:28 +02:00
// UPDATE users SET ('2008-01-01 01:08:10')
```
Testing:
```php
echo dibi::$sql; // last SQL query
echo dibi::$elapsedTime;
echo dibi::$numOfQueries;
echo dibi::$totalTime;
```