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

93 lines
2.2 KiB
PHP
Raw Normal View History

2010-08-03 12:28:07 +02:00
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2010-08-03 11:48:51 +02:00
<h1>Fetching Examples | dibi</h1>
2010-08-03 12:28:07 +02:00
2008-07-17 03:51:29 +00:00
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install dependencies using `composer install --dev`');
}
Tracy\Debugger::enable();
2008-07-17 03:51:29 +00:00
2015-10-06 01:39:01 +02:00
dibi::connect([
2015-06-19 03:11:36 +02:00
'driver' => 'sqlite3',
'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";
$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";
$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";
$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";
$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";
2014-05-13 19:02:19 +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";
2014-05-13 19:02:19 +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
$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";
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$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";
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$assoc = $res->fetchAssoc('name->title'); // key
Tracy\Dumper::dump($assoc);