MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file example.php

Documentation is available at example.php

  1. <?php
  2.  
  3.     // $Id: example.php,v 1.6 2004/04/09 10:42:36 lsmith Exp $
  4.     //
  5.     // MDB2 test script.
  6.     //
  7.  
  8.     // BC hack to define PATH_SEPARATOR for version of PHP prior 4.3
  9.     if (!defined('PATH_SEPARATOR')) {
  10.         if (defined('DIRECTORY_SEPARATOR'&& DIRECTORY_SEPARATOR == "\\"{
  11.             define('PATH_SEPARATOR'';');
  12.         else {
  13.             define('PATH_SEPARATOR'':');
  14.         }
  15.     }
  16.     ini_set('include_path''../..'.PATH_SEPARATOR.ini_get('include_path'));
  17.  
  18.     // MDB2.php doesnt have to be included since manager.php does that
  19.     // manager.php is only necessary for handling xml schema files
  20.     require_once 'MDB2.php';
  21.     // only including this to output result data
  22.     require_once 'Var_Dump.php';
  23.  
  24.     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'handle_pear_error');
  25.     function handle_pear_error ($error_obj)
  26.     {
  27.         print '<pre><b>PEAR-Error</b><br />';
  28.         echo $error_obj->getMessage().': '.$error_obj->getUserinfo();
  29.         print '</pre>';
  30.     }
  31.  
  32.     // just for kicks you can mess up this part to see some pear error handling
  33.     $user 'metapear';
  34.     $pass 'funky';
  35.     $host 'localhost';
  36.     $db_name 'metapear_test_db';
  37.     if (isset($_GET['db_type'])) {
  38.         $db_type $_GET['db_type'];
  39.     else {
  40.         $db_type 'mysql';
  41.     }
  42.     echo($db_type.'<br>');
  43.  
  44.     // Data Source Name: This is the universal connection string
  45.     $dsn['username'$user;
  46.     $dsn['password'$pass;
  47.     $dsn['hostspec'$host;
  48.     $dsn['phptype'$db_type;
  49.     // MDB2::connect will return a Pear DB object on success
  50.     // or a Pear MDB2 error object on error
  51.     // You can also set to true the second param
  52.     // if you want a persistent connection:
  53.     // $db = MDB2::connect($dsn, true);
  54.     // you can alternatively build a dsn here
  55.    //$dsn = "$db_type://$user:$pass@$host/$db_name";
  56.     Var_Dump::display($dsn);
  57.     $db =MDB2::connect($dsn);
  58.     // With MDB2::isError you can differentiate between an error or
  59.     // a valid connection.
  60.     if (MDB2::isError($db)) {
  61.         die (__LINE__.$db->getMessage());
  62.     }
  63.  
  64.     MDB2::loadFile('Tools/Manager');
  65.     $manager =new MDB2_Tools_Manager;
  66.     $input_file 'metapear_test_db.schema';
  67.     // you can either pass a dsn string, a dsn array or an exisiting db connection
  68.     $manager->connect($db);
  69.     // lets create the database using 'metapear_test_db.schema'
  70.     // if you have allready run this script you should have 'metapear_test_db.schema.before'
  71.     // in that case MDB2 will just compare the two schemas and make any necessary modifications to the existing DB
  72.     echo(Var_Dump::display($manager->updateDatabase($input_file$input_file.'.before')).'<br>');
  73.     echo('updating database from xml schema file<br>');
  74.  
  75.     echo('switching to database: '.$db_name.'<br>');
  76.     $db->setDatabase($db_name);
  77.     // happy query
  78.     $query ='SELECT * FROM test';
  79.     echo('query for the following examples:'.$query.'<br>');
  80.     // run the query and get a result handler
  81.     $result $db->query($query);
  82.     // lets just get row:0 column:0 and free the result
  83.     $field $result->fetch();
  84.     $result->free();
  85.     echo('<br>field:<br>'.$field.'<br>');
  86.     // run the query and get a result handler
  87.     $result $db->query($query);
  88.     // lets just get row:0 and free the result
  89.     $array $result->fetchRow();
  90.     $result->free();
  91.     echo('<br>row:<br>');
  92.     echo(Var_Dump::display($array).'<br>');
  93.     // run the query and get a result handler
  94.     $result $db->query($query);
  95.     // lets just get row:0 and free the result
  96.     $array $result->fetchRow();
  97.     $result->free();
  98.     echo('<br>row from object:<br>');
  99.     echo(Var_Dump::display($array).'<br>');
  100.     // run the query and get a result handler
  101.     $result $db->query($query);
  102.     // lets just get column:0 and free the result
  103.     $array $result->fetchCol(2);
  104.     $result->free();
  105.     echo('<br>get column #2 (counting from 0):<br>');
  106.     echo(Var_Dump::display($array).'<br>');
  107.     // run the query and get a result handler
  108.     $result $db->query($query);
  109.     Var_Dump::display($db->loadModule('reverse'));
  110.     echo('tableInfo:<br>');
  111.     echo(Var_Dump::display($db->reverse->tableInfo($result)).'<br>');
  112.     $types = array('integer''text''timestamp');
  113.     $result->setResultTypes($types);
  114.     $array $result->fetchAll(MDB2_FETCHMODE_FLIPPED);
  115.     $result->free();
  116.     echo('<br>all with result set flipped:<br>');
  117.     echo(Var_Dump::display($array).'<br>');
  118.     // save some time with this function
  119.     // lets just get all and free the result
  120.     $array $db->queryAll($query);
  121.     echo('<br>all with just one call:<br>');
  122.     echo(Var_Dump::display($array).'<br>');
  123.     // run the query with the offset 1 and count 1 and get a result handler
  124.     Var_Dump::display($db->loadModule('extended'));
  125.     $result $db->extended->limitQuery($querynull11);
  126.     // lets just get everything but with an associative array and free the result
  127.     $array $result->fetchAll(MDB2_FETCHMODE_ASSOC);
  128.     echo('<br>associative array with offset 1 and count 1:<br>');
  129.     echo(Var_Dump::display($array).'<br>');
  130.     // lets create a sequence
  131.     echo(Var_Dump::display($db->loadModule('manager')));
  132.     echo('<br>create a new seq with start 3 name real_funky_id<br>');
  133.     $err $db->manager->createSequence('real_funky_id'3);
  134.     if (MDB2::isError($err)) {
  135.             echo('<br>could not create sequence again<br>');
  136.     }
  137.     echo('<br>get the next id:<br>');
  138.     $value $db->nextId('real_funky_id');
  139.     echo($value.'<br>');
  140.     // lets try an prepare execute combo
  141.     $alldata = array(
  142.                      array(1'one''un'),
  143.                      array(2'two''deux'),
  144.                      array(3'three''trois'),
  145.                      array(4'four''quatre')
  146.     );
  147.     $prepared_query $db->prepare('INSERT INTO numbers VALUES(?,?,?)'array('integer''text''text'));
  148.     foreach ($alldata as $row{
  149.         echo('running execute<br>');
  150.         $db->executeParams($prepared_querynull$row);
  151.     }
  152.     // lets try an prepare execute combo
  153.     $alldata = array(
  154.                      array(5'five''cinq'),
  155.                      array(6'six''six'),
  156.                      array(7'seven''sept'),
  157.                      array(8'eight''huit')
  158.     );
  159.     $prepared_query $db->prepare('INSERT INTO numbers VALUES(?,?,?)'array('integer''text''text'));
  160.     echo('running executeMultiple<br>');
  161.     echo(Var_Dump::display($db->executeMultiple($prepared_querynull$alldata)).'<br>');
  162.     $array = array(4);
  163.     echo('<br>see getOne in action:<br>');
  164.     echo(Var_Dump::display($db->extended->getOne('SELECT trans_en FROM numbers WHERE number = ?',null,$array,'text')).'<br>');
  165.     $db->setFetchmode(MDB2_FETCHMODE_ASSOC);
  166.     echo('<br>default fetchmode ist now MDB2_FETCHMODE_ASSOC<br>');
  167.     echo('<br>see getRow in action:<br>');
  168.     echo(Var_Dump::display($db->extended->getRow('SELECT * FROM numbers WHERE number = ?',array('integer','text','text'),$array'integer')));
  169.     echo('default fetchmode ist now MDB2_FETCHMODE_ORDERED<br>');
  170.     $db->setFetchmode(MDB2_FETCHMODE_ORDERED);
  171.     echo('<br>see getCol in action:<br>');
  172.     echo(Var_Dump::display($db->extended->getCol('SELECT * FROM numbers WHERE number != ?',null,$array,'text'1)).'<br>');
  173.     echo('<br>see getAll in action:<br>');
  174.     echo(Var_Dump::display($db->extended->getAll('SELECT * FROM test WHERE test_id != ?',array('integer','text','text')$array'integer')).'<br>');
  175.     echo('<br>see getAssoc in action:<br>');
  176.     echo(Var_Dump::display($db->extended->getAssoc('SELECT * FROM test WHERE test_id != ?',array('integer','text','text')$array'integer'MDB2_FETCHMODE_ASSOC)).'<br>');
  177.     echo('tableInfo on a string:<br>');
  178.     echo(Var_Dump::display($db->reverse->tableInfo('numbers')).'<br>');
  179.     echo('<br>just a simple update query:<br>');
  180.     echo(Var_Dump::display($db->query('UPDATE numbers set trans_en ='.$db->quote(0'integer'))).'<br>');
  181.     echo('<br>affected rows:<br>');
  182.     echo($db->affectedRows().'<br>');
  183.     // subselect test
  184.     $sub_select $db->subSelect('SELECT test_name from test WHERE test_name = '.$db->quote('gummihuhn''text')'text');
  185.     echo(Var_Dump::display($sub_select).'<br>');
  186.     $query_with_subselect 'SELECT * FROM test WHERE test_name IN ('.$sub_select.')';
  187.     // run the query and get a result handler
  188.     echo($query_with_subselect.'<br>');
  189.     $result $db->query($query_with_subselect);
  190.     $array $result->fetchAll();
  191.     $result->free();
  192.     echo('<br>all with subselect:<br>');
  193.     echo('<br>drop index (will fail if the index was never created):<br>');
  194.     echo(Var_Dump::display($db->manager->dropIndex('test''test_id_index')).'<br>');
  195.     $index_def = array(
  196.         'fields' => array(
  197.             'test_id' => array(
  198.                 'sorting' => 'ascending'
  199.             )
  200.         )
  201.     );
  202.     echo('<br>create index:<br>');
  203.     echo(Var_Dump::display($db->manager->createIndex('test''test_id_index'$index_def)).'<br>');
  204.  
  205.     if ($db_type == 'mysql'{
  206.         $manager->db->setOption('debug'true);
  207.         $manager->db->setOption('log_line_break''<br>');
  208.         // ok now lets create a new xml schema file from the existing DB
  209.         // we will not use the 'metapear_test_db.schema' for this
  210.         // this feature is especially interesting for people that have an existing Db and want to move to MDB2's xml schema management
  211.         // you can also try MDB2_MANAGER_DUMP_ALL and MDB2_MANAGER_DUMP_CONTENT
  212.         echo(Var_Dump::display($manager->dumpDatabase(
  213.             array(
  214.                 'output_mode' => 'file',
  215.                 'output' => $db_name.'2.schema'
  216.             ),
  217.             MDB2_MANAGER_DUMP_STRUCTURE
  218.         )).'<br>');
  219.         if ($manager->db->getOption('debug'=== true{
  220.             echo($manager->debugOutput().'<br>');
  221.         }
  222.         // this is the database definition as an array
  223.         echo(Var_Dump::display($manager->database_definition).'<br>');
  224.     }
  225.  
  226.     echo('<br>just a simple delete query:<br>');
  227.     echo(Var_Dump::display($db->query('DELETE FROM numbers')).'<br>');
  228.     // You can disconnect from the database with:
  229.     $db->disconnect()
  230. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.