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:
57
examples/sql-condition.php
Normal file
57
examples/sql-condition.php
Normal 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'
|
||||
);
|
||||
|
||||
?>
|
Reference in New Issue
Block a user