1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-01 11:50:15 +02:00

update to 0.5alpha

This commit is contained in:
David Grudl
2006-06-04 23:06:33 +00:00
commit 0d18c4c366
21 changed files with 3384 additions and 0 deletions

40
examples/connect.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
require_once '../dibi/dibi.php';
// use two connections:
// first connection to mysql
$state = dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'charset' => 'utf8',
), 1);
if ($state instanceof Exception) {
echo $state;
}
if (!dibi::isConnected()) {
die();
}
// second connection to odbc
dibi::connect(array(
'driver' => 'odbc',
'username' => 'root',
'password' => '***',
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
), 3);
echo dibi::isConnected();
?>

52
examples/fetch.php Normal file
View File

@@ -0,0 +1,52 @@
<pre>
<?php
require_once '../dibi/dibi.php';
dibi::$debug = true;
// mysql
dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'charset' => 'utf8',
));
if (!dibi::isConnected())
die('Not connected');
$res = dibi::query('SELECT * FROM table');
// fetch a single value
$value = $res->fetchSingle();
// fetch complete result set
$all = $res->fetchAll();
// fetch complete result set like association array
$assoc = $res->fetchAll('id');
$assoc = $res->fetchAll('id', 'id2');
// fetch complete result set like pairs key => value
$pairs = $res->fetchPairs('id', 'name');
// fetch row by row
foreach ($res as $row => $fields) {
print_r($fields);
}
// fetch row by row with defined offset and limit
foreach ($res->getIterator(2, 3) as $row => $fields) {
print_r($fields);
}
?>

14
examples/log.sql Normal file
View File

@@ -0,0 +1,14 @@
Successfully connected to DB 'mysql'
SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
-- Result: object(DibiMySqlResult) rows: 1
-- Takes: 4.994 ms
SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
-- Result: object(DibiMySqlResult) rows: 29
-- Takes: 135.842 ms
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\dgx\*nucleus_item.frm' (errno: 22)
-- Takes: 121.454 ms

31
examples/logging.php Normal file
View File

@@ -0,0 +1,31 @@
<pre>
<?php
require_once '../dibi/dibi.php';
dibi::$logfile = 'log.sql';
// mysql
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'charset' => 'utf8',
));
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] = %i', 38);
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] < %i', 38);
$res = dibi::query('SELECT * FROM [*nucleus_item] WHERE [inumber] < %i', 38);
?>

34
examples/metatypes.php Normal file
View File

@@ -0,0 +1,34 @@
<pre>
<?php
require_once '../dibi/dibi.php';
// mysql
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'charset' => 'utf8',
));
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
// auto-convert this field to integer
$res->setType('inumber', DibiResult::FIELD_INTEGER);
$record = $res->fetch();
var_dump($record);
// auto-detect all types
$res->setType(TRUE);
$record = $res->fetch();
var_dump($record);
?>

48
examples/sql-builder.php Normal file
View File

@@ -0,0 +1,48 @@
<pre>
<?php
require_once '../dibi/dibi.php';
// mysql
dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'charset' => 'utf8',
));
$arr1 = array(1, 2, 3);
$arr2 = array('one', 'two', 'three');
$arr3 = array(
'a' => 'one',
'b' => 'two',
'c' => 'three',
);
$arr4 = array(
'A' => 12,
'B' => NULL,
'C' => new TDateTime(31542),
'D' => 'string',
);
dibi::test(
"
SELECT *
FROM [test]
WHERE ([test.a] LIKE %T", '1995-03-01', "
OR [b1] IN (", $arr1, ")
OR [b2] IN (", $arr2, ")
OR [b3] IN (%N", $arr3, ")
OR [b4] IN %V", $arr4, "
AND [c] = 'embedded '' string'
OR [d]=%d", 10.3, "
OR [true]=", true, "
OR [false]=", false, "
OR [null]=", NULL, "
LIMIT 10");
?>