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

Documentation generated on Mon, 11 Mar 2019 14:20:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.