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

Source for file peardb_wrapper_example.php

Documentation is available at peardb_wrapper_example.php

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

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