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

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