1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 02:12:25 +01:00
php-dibi/examples/fetch.php

95 lines
1.8 KiB
PHP
Raw Normal View History

2008-07-17 03:51:29 +00:00
<h1>dibi fetch example</h1>
<pre>
<?php
require_once 'Nette/Debug.php';
2008-07-17 03:51:29 +00:00
require_once '../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'data/sample.sdb',
2008-07-17 03:51:29 +00:00
));
/*
TABLE products
product_id | title
-----------+----------
1 | Chair
2 | Table
3 | Computer
*/
// fetch a single row
$row = dibi::fetch('SELECT title FROM [products]');
Debug::dump($row); // Chair
2008-07-17 03:51:29 +00:00
echo '<hr>';
// fetch a single value
$value = dibi::fetchSingle('SELECT [title] FROM [products]');
Debug::dump($value); // Chair
2008-07-17 03:51:29 +00:00
echo '<hr>';
// fetch complete result set
$all = dibi::fetchAll('SELECT * FROM [products]');
Debug::dump($all);
2008-07-17 03:51:29 +00:00
echo '<hr>';
// fetch complete result set like association array
$res = dibi::query('SELECT * FROM [products]');
$assoc = $res->fetchAssoc('title'); // key
Debug::dump($assoc);
2008-07-17 03:51:29 +00:00
echo '<hr>';
// fetch complete result set like pairs key => value
$pairs = $res->fetchPairs('product_id', 'title');
Debug::dump($pairs);
2008-07-17 03:51:29 +00:00
echo '<hr>';
// fetch row by row
foreach ($res as $n => $row) {
Debug::dump($row);
2008-07-17 03:51:29 +00:00
}
echo '<hr>';
// fetch row by row with defined offset
foreach ($res->getIterator(2) as $n => $row) {
Debug::dump($row);
2008-07-17 03:51:29 +00:00
}
// fetch row by row with defined offset and limit
foreach ($res->getIterator(2, 1) as $n => $row) {
Debug::dump($row);
2008-07-17 03:51:29 +00:00
}
// more complex association array
$res = dibi::query('
SELECT *
FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
');
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
Debug::dump($assoc);
2008-07-17 03:51:29 +00:00
echo '<hr>';
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
Debug::dump($assoc);
2008-07-17 03:51:29 +00:00
echo '<hr>';
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
Debug::dump($assoc);
2008-07-17 03:51:29 +00:00
echo '<hr>';