1
0
mirror of https://github.com/dg/dibi.git synced 2025-01-17 14:18:25 +01:00
php-dibi/examples/query-language-and-conditions.php

59 lines
1010 B
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>Query Language & Conditions | dibi</h1>
2010-08-03 12:28:07 +02:00
2008-07-17 03:51:29 +00:00
<?php
2011-07-01 08:06:36 +02:00
require_once 'Nette/Debugger.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
));
2010-08-03 11:48:51 +02:00
// some variables
2008-10-28 02:06:55 +00:00
$cond1 = TRUE;
$cond2 = FALSE;
2008-07-17 03:51:29 +00:00
$foo = -1;
$bar = 2;
2010-08-03 11:48:51 +02:00
// conditional variable
2008-07-17 03:51:29 +00:00
$name = $cond1 ? 'K%' : NULL;
// if & end
dibi::test('
2010-08-03 11:48:51 +02:00
SELECT *
FROM customers
%if', isset($name), 'WHERE name LIKE %s', $name, '%end'
2008-07-17 03:51:29 +00:00
);
// -> SELECT * FROM customers WHERE name LIKE 'K%'
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
// if & else & (optional) end
dibi::test("
2010-08-03 11:48:51 +02:00
SELECT *
FROM people
WHERE id > 0
%if", ($foo > 0), "AND foo=%i", $foo, "
%else %if", ($bar > 0), "AND bar=%i", $bar, "
2008-07-17 03:51:29 +00:00
");
// -> SELECT * FROM people WHERE id > 0 AND bar=2
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
// nested condition
dibi::test('
2010-08-03 11:48:51 +02:00
SELECT *
FROM customers
2010-08-03 11:48:51 +02:00
WHERE
%if', isset($name), 'name LIKE %s', $name, '
%if', $cond2, 'AND admin=1 %end
2010-08-03 11:48:51 +02:00
%else 1 LIMIT 10 %end'
2008-07-17 03:51:29 +00:00
);
// -> SELECT * FROM customers WHERE LIMIT 10