mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 14:16:39 +02:00
bugfix, enhanced error reporting, better examples
This commit is contained in:
@@ -5,6 +5,12 @@ require_once '../dibi/dibi.php';
|
||||
|
||||
try {
|
||||
|
||||
// connects to SQlite
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
// connects to MySQL using DSN
|
||||
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
|
||||
|
||||
@@ -29,13 +35,6 @@ try {
|
||||
));
|
||||
|
||||
|
||||
// connects to SQlite
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'mydb.sdb',
|
||||
));
|
||||
|
||||
|
||||
// connects to PostgreSql
|
||||
dibi::connect(array(
|
||||
'driver' => 'postgre',
|
||||
|
@@ -6,19 +6,18 @@ if (function_exists('date_default_timezone_set'))
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [mytable]');
|
||||
$res = dibi::query('
|
||||
SELECT * FROM [products]
|
||||
INNER JOIN [orders] USING ([product_id])
|
||||
INNER JOIN [customers] USING ([customer_id])
|
||||
');
|
||||
|
||||
// get last SQL
|
||||
$sql = dibi::$sql;
|
||||
|
@@ -4,42 +4,72 @@
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [mytable]');
|
||||
if (!$res) die('SQL error');
|
||||
/*
|
||||
TABLE products
|
||||
|
||||
product_id | title
|
||||
-----------+----------
|
||||
1 | Chair
|
||||
2 | Table
|
||||
3 | Computer
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// fetch a single value
|
||||
$res = dibi::query('SELECT [title] FROM [products]');
|
||||
if (!$res) die('SQL error');
|
||||
|
||||
$value = $res->fetchSingle();
|
||||
print_r($value); // Chair
|
||||
echo '<hr>';
|
||||
|
||||
|
||||
// fetch complete result set
|
||||
$res = dibi::query('SELECT * FROM [products]');
|
||||
$all = $res->fetchAll();
|
||||
print_r($all);
|
||||
echo '<hr>';
|
||||
|
||||
|
||||
// fetch complete result set like association array
|
||||
$assoc = $res->fetchAssoc('id');
|
||||
$assoc = $res->fetchAssoc('title'); // key
|
||||
print_r($assoc);
|
||||
echo '<hr>';
|
||||
|
||||
$assoc = $res->fetchAssoc('id', 'id2');
|
||||
|
||||
// fetch complete result set like pairs key => value
|
||||
$pairs = $res->fetchPairs('id', 'name');
|
||||
$pairs = $res->fetchPairs('product_id', 'title');
|
||||
print_r($pairs);
|
||||
echo '<hr>';
|
||||
|
||||
|
||||
// fetch row by row
|
||||
foreach ($res as $row => $fields) {
|
||||
print_r($fields);
|
||||
}
|
||||
echo '<hr>';
|
||||
|
||||
|
||||
// fetch row by row with defined offset and limit
|
||||
foreach ($res->getIterator(2, 3) as $row => $fields) {
|
||||
foreach ($res->getIterator(2, 1) as $row => $fields) {
|
||||
print_r($fields);
|
||||
}
|
||||
|
||||
|
||||
// more complex association array
|
||||
$res = dibi::query('
|
||||
SELECT * FROM [products]
|
||||
INNER JOIN [orders] USING ([product_id])
|
||||
INNER JOIN [customers] USING ([customer_id])
|
||||
');
|
||||
|
||||
$assoc = $res->fetchAssoc('customers.name,products.title'); // key
|
||||
print_r($assoc);
|
||||
echo '<hr>';
|
||||
|
154
examples/log.sql
154
examples/log.sql
@@ -1,140 +1,24 @@
|
||||
OK: connected to DB 'mysql'
|
||||
OK: connected to DB 'sqlite'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.109 ms
|
||||
-- 2007-02-02 04:38:34
|
||||
OK: SELECT * FROM [customers] WHERE [customer_id] = 1;
|
||||
-- result: object(DibiSqliteResult) rows: 1
|
||||
-- takes: 0.331 ms
|
||||
-- driver: sqlite
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.492 ms
|
||||
-- 2007-02-02 04:38:34
|
||||
OK: SELECT * FROM [customers] WHERE [customer_id] < 5;
|
||||
-- result: object(DibiSqliteResult) rows: 4
|
||||
-- takes: 0.324 ms
|
||||
-- driver: sqlite
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:38:34
|
||||
ERROR: [1] SQL logic error or missing database
|
||||
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 5
|
||||
-- driver: sqlite;
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:38:34
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.565 ms
|
||||
-- 2007-02-02 04:47:37
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.347 ms
|
||||
-- 2007-02-02 04:47:37
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:47:37
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:47:37
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.147 ms
|
||||
-- 2007-02-02 04:47:56
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.085 ms
|
||||
-- 2007-02-02 04:47:56
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:47:56
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:47:57
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 0.944 ms
|
||||
-- 2007-02-02 04:48:23
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.258 ms
|
||||
-- 2007-02-02 04:48:23
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:48:23
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:48:23
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.039 ms
|
||||
-- 2007-02-02 04:50:46
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.499 ms
|
||||
-- 2007-02-02 04:50:46
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:50:46
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-02-02 04:50:46
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.518 ms
|
||||
-- 2007-03-28 00:45:27
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 6.385 ms
|
||||
-- 2007-03-28 00:45:27
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-03-28 00:45:27
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-03-28 00:45:27
|
||||
|
||||
OK: connected to DB 'mysql'
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
|
||||
-- result: object(DibiMySqlResult) rows: 0
|
||||
-- takes: 1.124 ms
|
||||
-- 2007-03-28 01:06:07
|
||||
|
||||
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
|
||||
-- result: object(DibiMySqlResult) rows: 12
|
||||
-- takes: 1.607 ms
|
||||
-- 2007-03-28 01:06:07
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-03-28 01:06:07
|
||||
|
||||
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
|
||||
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
|
||||
-- 2007-03-28 01:06:07
|
||||
ERROR: [1] SQL logic error or missing database
|
||||
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 38
|
||||
-- driver: sqlite;
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
|
@@ -19,14 +19,9 @@ dibi::$logAll = TRUE;
|
||||
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
@@ -36,13 +31,13 @@ dibi::$throwExceptions = FALSE;
|
||||
echo '<h1>User-level errors</h1>';
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] = %i', 38);
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] = %i', 1);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] < %i', 38);
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', 5);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
|
||||
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 5);
|
||||
|
||||
echo "<br />See file ", dibi::$logFile;
|
||||
|
||||
@@ -54,7 +49,7 @@ echo '<h1>DibiException</h1>';
|
||||
|
||||
try {
|
||||
|
||||
$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
|
||||
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);
|
||||
|
||||
} catch (DibiException $e) {
|
||||
|
||||
|
@@ -4,28 +4,24 @@
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] <> %i', 38);
|
||||
$res = dibi::query('SELECT * FROM [customers]');
|
||||
if (!$res) die('SQL error');
|
||||
|
||||
|
||||
// auto-convert this field to integer
|
||||
$res->setType('inumber', Dibi::FIELD_INTEGER);
|
||||
$res->setType('customer_id', Dibi::FIELD_INTEGER);
|
||||
$record = $res->fetch();
|
||||
var_dump($record);
|
||||
|
||||
|
||||
// auto-detect all types
|
||||
// WARNING: THIS WILL NOT WORK WITH SQLITE
|
||||
$res->setType(TRUE);
|
||||
$record = $res->fetch();
|
||||
var_dump($record);
|
||||
|
BIN
examples/sample.sdb
Normal file
BIN
examples/sample.sdb
Normal file
Binary file not shown.
@@ -11,14 +11,9 @@ if (function_exists('date_default_timezone_set'))
|
||||
date_default_timezone_set('Europe/Prague'); // or 'GMT'
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
@@ -7,14 +7,9 @@ pre.dibi { padding-bottom: 10px; }
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
@@ -22,29 +17,29 @@ $cond1 = rand(0,2) < 1;
|
||||
$cond2 = rand(0,2) < 1;
|
||||
|
||||
|
||||
$user = $cond1 ? 'Davidek' : NULL;
|
||||
|
||||
$name = $cond1 ? 'K%' : NULL;
|
||||
|
||||
// if & end
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM [mytable]
|
||||
%if', isset($user), 'WHERE [user]=%s', $user, '%end'
|
||||
FROM [customers]
|
||||
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
|
||||
);
|
||||
|
||||
|
||||
// last end is optional
|
||||
// if & else & end (last end is optional)
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM %if', $cond1, '[one_table] %else [second_table]'
|
||||
FROM %if', $cond1, '[customers] %else [products]'
|
||||
);
|
||||
|
||||
|
||||
// nested condition
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM [mytable]
|
||||
FROM [customers]
|
||||
WHERE
|
||||
%if', isset($user), '[user]=%s', $user, '
|
||||
%if', isset($name), '[name] LIKE %s', $name, '
|
||||
%if', $cond2, 'AND [admin]=1 %end
|
||||
%else LIMIT 10 %end'
|
||||
);
|
||||
|
@@ -3,14 +3,9 @@
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// CHANGE TO REAL PARAMETERS!
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user