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

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