1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 13:17:58 +02:00

added conditional SQL

This commit is contained in:
David Grudl
2006-06-07 19:02:24 +00:00
parent 0951ea574c
commit a3a17f1c55
4 changed files with 128 additions and 35 deletions

View File

@@ -0,0 +1,57 @@
<style>
pre.dibi { padding-bottom: 10px; }
</style>
<pre>
<?php
require_once '../dibi/dibi.php';
// mysql
dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx', // change to real password!
'charset' => 'utf8',
));
$user = NULL;
// or
$user = 'Jesus';
dibi::test('
SELECT *
FROM [test]
WHERE %if', isset($user), 'user=%s', $user, '%end' // last end is optional
);
$cond = rand(0,2) < 1;
dibi::test('
SELECT *
FROM %if', $cond, '[one_table]', '%else', '[second_table]', '%end'
);
// shorter way
dibi::test('
SELECT *
FROM %if', $cond, '[one_table] %else', '[second_table] %end'
);
// nested condition
dibi::test('
SELECT *
FROM [test]
WHERE
%if', isset($user), 'user=%s', $user, '
%if', $cond, 'AND [admin]=1 %end', '
AND [visible]=1 %end'
);
?>