2008-07-17 03:51:29 +00:00
|
|
|
<h1>dibi fetch example</h1>
|
|
|
|
<pre>
|
|
|
|
<?php
|
|
|
|
|
2008-10-28 19:31:32 +00:00
|
|
|
require_once 'Nette/Debug.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
require_once '../dibi/dibi.php';
|
|
|
|
|
|
|
|
|
|
|
|
dibi::connect(array(
|
|
|
|
'driver' => 'sqlite',
|
2010-08-03 12:11:14 +02:00
|
|
|
'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]');
|
2008-10-28 19:31:32 +00:00
|
|
|
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]');
|
2008-10-28 19:31:32 +00:00
|
|
|
Debug::dump($value); // Chair
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<hr>';
|
|
|
|
|
|
|
|
|
|
|
|
// fetch complete result set
|
|
|
|
$all = dibi::fetchAll('SELECT * FROM [products]');
|
2008-10-28 19:31:32 +00:00
|
|
|
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
|
2008-10-28 19:31:32 +00:00
|
|
|
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');
|
2008-10-28 19:31:32 +00:00
|
|
|
Debug::dump($pairs);
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<hr>';
|
|
|
|
|
|
|
|
|
|
|
|
// fetch row by row
|
|
|
|
foreach ($res as $n => $row) {
|
2008-10-28 19:31:32 +00:00
|
|
|
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) {
|
2008-10-28 19:31:32 +00:00
|
|
|
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) {
|
2008-10-28 19:31:32 +00:00
|
|
|
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])
|
|
|
|
');
|
|
|
|
|
2009-10-06 18:09:13 +02:00
|
|
|
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
|
2008-10-28 19:31:32 +00:00
|
|
|
Debug::dump($assoc);
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<hr>';
|
|
|
|
|
2009-10-06 18:09:13 +02:00
|
|
|
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
|
2008-10-28 19:31:32 +00:00
|
|
|
Debug::dump($assoc);
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<hr>';
|
|
|
|
|
2009-10-06 18:09:13 +02:00
|
|
|
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
|
2008-10-28 19:31:32 +00:00
|
|
|
Debug::dump($assoc);
|
2008-07-17 03:51:29 +00:00
|
|
|
echo '<hr>';
|