1
0
mirror of https://github.com/dg/dibi.git synced 2025-03-11 01:39:49 +01:00
php-dibi/examples/sql-condition.php

57 lines
975 B
PHP
Raw Normal View History

2010-08-03 12:28:07 +02:00
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2008-07-17 03:51:29 +00:00
<h1>dibi conditional SQL example</h1>
2010-08-03 12:28:07 +02:00
2008-07-17 03:51:29 +00:00
<?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
));
2008-10-28 02:06:55 +00:00
$cond1 = TRUE;
$cond2 = FALSE;
2008-07-17 03:51:29 +00:00
$foo = -1;
$bar = 2;
$name = $cond1 ? 'K%' : NULL;
// if & end
dibi::test('
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
);
2008-10-28 02:06:55 +00:00
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
2008-07-17 03:51:29 +00:00
// if & else & (optional) end
dibi::test("
SELECT *
FROM [people]
WHERE [id] > 0
%if", ($foo > 0), "AND [foo]=%i", $foo, "
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
");
2008-10-28 02:06:55 +00:00
// -> SELECT * FROM [people] WHERE [id] > 0 AND [bar]=2
2008-07-17 03:51:29 +00:00
// nested condition
dibi::test('
SELECT *
FROM [customers]
WHERE
%if', isset($name), '[name] LIKE %s', $name, '
%if', $cond2, 'AND [admin]=1 %end
%else 1 LIMIT 10 %end'
);
2008-10-28 02:06:55 +00:00
// -> SELECT * FROM [customers] WHERE LIMIT 10