1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 10:26:21 +01:00
php-dibi/examples/sql-condition.php

51 lines
834 B
PHP
Raw Normal View History

2006-06-07 19:02:24 +00:00
<style>
pre.dibi { padding-bottom: 10px; }
</style>
<pre>
<?php
require_once '../dibi/dibi.php';
// CHANGE TO REAL PARAMETERS!
2006-06-07 19:02:24 +00:00
dibi::connect(array(
'driver' => 'mysql',
2006-06-07 19:02:24 +00:00
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx',
'database' => 'dibi',
2006-06-07 19:02:24 +00:00
'charset' => 'utf8',
));
2006-06-08 02:02:05 +00:00
$cond1 = rand(0,2) < 1;
$cond2 = rand(0,2) < 1;
2006-06-07 19:02:24 +00:00
2006-06-07 21:33:46 +00:00
$user = $cond1 ? 'Davidek' : NULL;
2006-06-07 19:02:24 +00:00
dibi::test('
SELECT *
FROM [mytable]
2006-06-07 21:33:46 +00:00
%if', isset($user), 'WHERE [user]=%s', $user, '%end'
2006-06-07 19:02:24 +00:00
);
2006-06-07 21:33:46 +00:00
// last end is optional
2006-06-07 19:02:24 +00:00
dibi::test('
SELECT *
2006-06-07 21:33:46 +00:00
FROM %if', $cond1, '[one_table] %else [second_table]'
2006-06-07 19:02:24 +00:00
);
// nested condition
dibi::test('
SELECT *
FROM [mytable]
2006-06-08 02:02:05 +00:00
WHERE
2006-06-07 21:33:46 +00:00
%if', isset($user), '[user]=%s', $user, '
%if', $cond2, 'AND [admin]=1 %end
%else LIMIT 10 %end'
2006-06-07 19:02:24 +00:00
);