1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 01:48:05 +01:00
php-dibi/examples/fetching-examples.php

96 lines
2.3 KiB
PHP
Raw Normal View History

2008-07-17 03:51:29 +00:00
<?php
2017-06-09 22:20:47 +02:00
declare(strict_types=1);
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install dependencies using `composer install --dev`');
}
Tracy\Debugger::enable();
2008-07-17 03:51:29 +00:00
?>
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2018-05-10 21:45:11 +02:00
<h1>Fetching Examples | Dibi</h1>
<?php
2008-07-17 03:51:29 +00:00
2018-04-17 10:03:43 +02:00
$dibi = new Dibi\Connection([
'driver' => 'sqlite',
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
/*
TABLE products
product_id | title
-----------+----------
1 | Chair
2 | Table
3 | Computer
*/
// fetch a single row
2010-08-03 11:48:51 +02:00
echo "<h2>fetch()</h2>\n";
2018-04-17 10:03:43 +02:00
$row = $dibi->fetch('SELECT title FROM products');
Tracy\Dumper::dump($row); // Chair
2008-07-17 03:51:29 +00:00
// fetch a single value
2010-08-03 11:48:51 +02:00
echo "<h2>fetchSingle()</h2>\n";
2018-04-17 10:03:43 +02:00
$value = $dibi->fetchSingle('SELECT title FROM products');
Tracy\Dumper::dump($value); // Chair
2008-07-17 03:51:29 +00:00
// fetch complete result set
2010-08-03 11:48:51 +02:00
echo "<h2>fetchAll()</h2>\n";
2018-04-17 10:03:43 +02:00
$all = $dibi->fetchAll('SELECT * FROM products');
Tracy\Dumper::dump($all);
2008-07-17 03:51:29 +00:00
// fetch complete result set like association array
2010-08-03 11:48:51 +02:00
echo "<h2>fetchAssoc('title')</h2>\n";
2018-04-17 10:03:43 +02:00
$res = $dibi->query('SELECT * FROM products');
2008-07-17 03:51:29 +00:00
$assoc = $res->fetchAssoc('title'); // key
Tracy\Dumper::dump($assoc);
2008-07-17 03:51:29 +00:00
// fetch complete result set like pairs key => value
2010-08-03 11:48:51 +02:00
echo "<h2>fetchPairs('product_id', 'title')</h2>\n";
2018-04-17 10:03:43 +02:00
$res = $dibi->query('SELECT * FROM products');
2008-07-17 03:51:29 +00:00
$pairs = $res->fetchPairs('product_id', 'title');
Tracy\Dumper::dump($pairs);
2008-07-17 03:51:29 +00:00
// fetch row by row
2010-08-03 11:48:51 +02:00
echo "<h2>using foreach</h2>\n";
2018-04-17 10:03:43 +02:00
$res = $dibi->query('SELECT * FROM products');
2008-07-17 03:51:29 +00:00
foreach ($res as $n => $row) {
Tracy\Dumper::dump($row);
2008-07-17 03:51:29 +00:00
}
// more complex association array
2018-04-17 10:03:43 +02:00
$res = $dibi->query('
2010-08-03 11:48:51 +02:00
SELECT *
FROM products
INNER JOIN orders USING (product_id)
INNER JOIN customers USING (customer_id)
2008-07-17 03:51:29 +00:00
');
2014-05-13 19:02:19 +02:00
echo "<h2>fetchAssoc('name|title')</h2>\n";
$assoc = $res->fetchAssoc('name|title'); // key
Tracy\Dumper::dump($assoc);
2008-07-17 03:51:29 +00:00
2014-05-13 19:02:19 +02:00
echo "<h2>fetchAssoc('name[]title')</h2>\n";
2018-04-17 10:03:43 +02:00
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
2014-05-13 19:02:19 +02:00
$assoc = $res->fetchAssoc('name[]title'); // key
Tracy\Dumper::dump($assoc);
2008-07-17 03:51:29 +00:00
2014-05-13 19:02:19 +02:00
echo "<h2>fetchAssoc('name->title')</h2>\n";
2018-04-17 10:03:43 +02:00
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
2014-05-13 19:02:19 +02:00
$assoc = $res->fetchAssoc('name->title'); // key
Tracy\Dumper::dump($assoc);