| 1 |
lars |
1 |
<pre>
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/**************************************/
|
|
|
5 |
/* a nice php5 only show case of MDB2 */
|
|
|
6 |
/**************************************/
|
|
|
7 |
|
|
|
8 |
require 'MDB2.php';
|
|
|
9 |
|
|
|
10 |
// the database needs to be created manually beforehand
|
|
|
11 |
$dsn = array(
|
|
|
12 |
'phptype' => 'pgsql',
|
|
|
13 |
'username' => 'postgres',
|
|
|
14 |
# 'phptype' => 'mysql',
|
|
|
15 |
# 'username' => 'root',
|
|
|
16 |
'password' => 'test',
|
|
|
17 |
'hostspec' => 'localhost',
|
|
|
18 |
'database' => 'driver_test',
|
|
|
19 |
);
|
|
|
20 |
#$dsn = 'sqlite:///:memory:';
|
|
|
21 |
|
|
|
22 |
// create MDB2 instance
|
|
|
23 |
$mdb2 = MDB2::factory($dsn);
|
|
|
24 |
if (PEAR::isError($mdb2)) {
|
|
|
25 |
die($mdb2->getMessage());
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
// set the default fetchmode
|
|
|
29 |
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
|
|
|
30 |
|
|
|
31 |
$fields = array(
|
|
|
32 |
'id' => array(
|
|
|
33 |
'type' => 'integer',
|
|
|
34 |
'unsigned' => true,
|
|
|
35 |
'autoincrement' => true,
|
|
|
36 |
),
|
|
|
37 |
'somename' => array(
|
|
|
38 |
'type' => 'text',
|
|
|
39 |
'length' => 12,
|
|
|
40 |
),
|
|
|
41 |
'somedate' => array(
|
|
|
42 |
'type' => 'date',
|
|
|
43 |
),
|
|
|
44 |
);
|
|
|
45 |
$table = 'sometable';
|
|
|
46 |
|
|
|
47 |
// create a table
|
|
|
48 |
// since we are on php5 we can use the magic __call() method to:
|
|
|
49 |
// - load the manager module: $mdb2->loadModule('Manager', null, true);
|
|
|
50 |
// - redirect the method call to the manager module: $mdb2->manager->createTable('sometable', $fields);
|
|
|
51 |
$mdb2->mgCreateTable($table, $fields);
|
|
|
52 |
|
|
|
53 |
$query = "INSERT INTO $table (somename, somedate) VALUES (:name, :date)";
|
|
|
54 |
// parameters:
|
|
|
55 |
// 1) the query (notice we are using named parameters, but we could also use ? instead
|
|
|
56 |
// 2) types of the placeholders (either keyed numerically in order or by name)
|
|
|
57 |
// 3) MDB2_PREPARE_MANIP denotes a DML statement
|
|
|
58 |
$stmt = $mdb2->prepare($query, array('text', 'date'), MDB2_PREPARE_MANIP);
|
|
|
59 |
if (PEAR::isError($stmt)) {
|
|
|
60 |
die($stmt->getMessage());
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// load Date helper class
|
|
|
64 |
MDB2::loadFile('Date');
|
|
|
65 |
|
|
|
66 |
$stmt->execute(array('name' => 'hello', 'date' => MDB2_Date::mdbToday()));
|
|
|
67 |
// get the last inserted id
|
|
|
68 |
echo 'last insert id: ';
|
|
|
69 |
var_dump($mdb2->lastInsertId($table, 'id'));
|
|
|
70 |
$stmt->execute(array('name' => 'world', 'date' => '2005-11-11'));
|
|
|
71 |
// get the last inserted id
|
|
|
72 |
echo 'last insert id: ';
|
|
|
73 |
var_dump($mdb2->lastInsertId($table, 'id'));
|
|
|
74 |
|
|
|
75 |
// load Iterator implementations
|
|
|
76 |
MDB2::loadFile('Iterator');
|
|
|
77 |
|
|
|
78 |
$query = 'SELECT * FROM '.$table;
|
|
|
79 |
// parameters:
|
|
|
80 |
// 1) the query
|
|
|
81 |
// 2) true means MDB2 tries to determine the result set type automatically
|
|
|
82 |
// 3) true is the default and means that internally a MDB2_Result instance should be created
|
|
|
83 |
// 4) 'MDB2_BufferedIterator' means the MDB2_Result should be wrapped inside an SeekableIterator
|
|
|
84 |
$result = $mdb2->query($query, true, true, 'MDB2_BufferedIterator');
|
|
|
85 |
|
|
|
86 |
// iterate over the result set
|
|
|
87 |
foreach ($result as $row) {
|
|
|
88 |
echo 'output row:<br>';
|
|
|
89 |
var_dump($row);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// call drop table, since dropTable is not implemented in our instance
|
|
|
93 |
// but inside the loaded Manager module __call() will find it there and
|
|
|
94 |
// will redirect the call accordingly
|
|
|
95 |
// we could also have done:
|
|
|
96 |
// $mdb2->manager->dropTable($table); or
|
|
|
97 |
// $mdb2->mgDropTable($table);
|
|
|
98 |
$mdb2->dropTable($table);
|
|
|
99 |
|
|
|
100 |
?>
|