1<?php
2// $Id: MDB_pear_wrapper_test.php,v 1.21 2003/01/18 21:35:19 lsmith Exp $
3//
4// MDB test script for the PEAR DB Wrapper.
5//
6
7// BC hack to define PATH_SEPARATOR for version of PHP prior 4.3
8if(!defined('PATH_SEPARATOR')) {
9    if(defined('DIRECTORY_SEPARATOR') && DIRECTORY_SEPARATOR == "\\") {
10        define('PATH_SEPARATOR', ';');
11    } else {
12        define('PATH_SEPARATOR', ':');
13    }
14}
15ini_set('include_path', '..'.PATH_SEPARATOR.ini_get('include_path'));
16
17    require_once('MDB.php');
18    MDB::loadFile('peardb_wrapper');
19    require_once('Var_Dump.php');
20
21    // just for kicks you can mess up this part to see some pear error handling
22    $user = 'metapear';
23    $pass = 'funky';
24    //$pass = '';
25    $host = 'localhost';
26    $db_name = 'metapear_test_db';
27    // Data Source Name: This is the universal connection string
28    $dsn = "mysql://$user:$pass@$host/$db_name";
29    // MDB::connect will return a Pear DB object on success
30    // or a Pear DB Error object on error
31    // You can also set to TRUE the second param
32    // if you want a persistent connection:
33    // $db = DB::connect($dsn, TRUE);
34    $db =& DB::connect($dsn);
35    // With DB::isError you can differentiate between an error or
36    // a valid connection.
37    //echo(Var_Dump::display($db).'<br>');
38    if (DB::isError($db)) {
39        die (__LINE__.$db->getMessage());
40    }
41
42    // happy query
43    $query ='SELECT * FROM test';
44    echo('query for the following examples:'.$query.'<br>');
45    echo('<br>field:<br>'.$db->getOne($query).'<br>');
46
47    // run the query and get a result handler
48    $result = $db->simpleQuery($query);
49    echo('<br>tableInfo() ');
50    Var_Dump::display($db->tableInfo($result));
51
52    $result = $db->query($query);
53    echo('<br>numCols() ');
54    Var_Dump::display($result->numCols());
55    $result->fetchInto($arr);
56    echo('<br>fetchInto() ');
57    Var_Dump::display($arr);
58    echo('<br>free() ');
59    Var_Dump::display($result->free());
60
61    $result = $db->query($query);
62    echo('<br>numRows() ');
63    Var_Dump::display($result->numRows());
64    echo('<br>fetchRow() ');
65    Var_Dump::display($result->fetchRow());
66
67    // lets create a sequence on demand
68    echo('<br>get the next id using on demand:<br>');
69    echo('<br>nextId:'.$db->nextId('real_funky_id_2'));
70    echo('<br>dropSequence:'.$db->dropSequence('real_funky_id_2'));
71    // lets create a sequence
72    echo('<br>create a new seq with start 3 name real_funky_id<br>');
73    $err = $db->createSequence('real_funky_id',3);
74    if (DB::isError($err)) {
75        echo('<br>could not create sequence again<br>');
76    }
77
78    echo('<br>get the next id:<br>');
79    echo($db->nextId('real_funky_id').'<br>');
80    // lets try an prepare execute combo
81    $alldata = array(  array(1, 'one', 'un'),
82                       array(2, 'two', 'deux'),
83                       array(3, 'three', 'trois'),
84                       array(4, 'four', 'quatre'));
85    $prepared_query = $db->prepare('INSERT INTO numbers VALUES(?,?,?)');
86    foreach ($alldata as $row) {
87        echo('running execute<br>');
88        $db->execute($prepared_query, $row);
89    }
90    // lets try an prepare execute combo
91    $alldata = array(  array(5, 'five', 'cinq'),
92                       array(6, 'six', 'six'),
93                       array(7, 'seven', 'sept'),
94                       array(8, 'eight', 'huit'));
95    $prepared_query = $db->prepare('INSERT INTO numbers VALUES(?,?,?)');
96    $db->executeMultiple($prepared_query, $alldata);
97    echo('running executeMultiple<br>');
98    $array = array(4);
99    echo('<br>see getOne in action:<br>'.$db->getOne('SELECT trans_en FROM numbers WHERE number = ?',$array).'<br>');
100    // You can disconnect from the database with:
101    echo('<br>see getRow in action:<br>');
102    echo(Var_Dump::display($db->getRow('SELECT * FROM numbers WHERE number = ?',$array)).'<br>');
103    echo('<br>see getCol in action:<br>');
104    echo(Var_Dump::display($db->getCol('SELECT * FROM numbers', 1)).'<br>');
105    echo('<br>see getAll in action:<br>');
106    echo(Var_Dump::display($db->getAll('SELECT * FROM test')).'<br>');
107    echo('<br>see getAssoc in action:<br>');
108    echo(Var_Dump::display($db->getAssoc('SELECT * FROM test', FALSE, '', DB_FETCHMODE_ASSOC)).'<br>');
109    echo('tableInfo on a string:<br>');
110    echo(Var_Dump::display($db->tableInfo('numbers')).'<br>');
111    echo('<br>just a simple delete query:<br>');
112    echo(Var_Dump::display($db->query('UPDATE numbers set trans_en = 0')).'<br>');
113    echo('<br>affected rows:<br>');
114    echo($db->affectedRows().'<br>');
115    echo('<br>just a simple delete query:<br>');
116    echo(Var_Dump::display($db->query('DELETE FROM numbers')).'<br>');
117    $db->disconnect();
118?>
119