cvs: pear /MDB MDB_test.php

From: Date: Wed, 12 Jun 2002 13:10:54 +0000
Subject: cvs: pear /MDB MDB_test.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4572@lists.php.net to get a copy of this message
lsmith Wed Jun 12 09:10:54 2002 EDT Modified files: /pear/MDB MDB_test.php Log: added a sample delete statement changed some formatting and improved comments

Index: pear/MDB/MDB_test.php diff -u pear/MDB/MDB_test.php:1.9 pear/MDB/MDB_test.php:1.10 --- pear/MDB/MDB_test.php:1.9 Wed Jun 5 10:35:41 2002 +++ pear/MDB/MDB_test.php Wed Jun 12 09:10:54 2002 @@ -1,47 +1,53 @@ <?php /* * - * @(#) $Header: /usr/repository/pear/MDB/MDB_test.php,v 1.9 2002/06/05 14:35:41 lsmith Exp $ + * @(#) $Header: /usr/repository/pear/MDB/MDB_test.php,v 1.10 2002/06/12 13:10:54 lsmith Exp $ * */ + // MDB.php doesnt have to be included since manager.php does that + // manager.php is only necessary for handling xml schema files require_once("manager.php"); + // only including this to output result data require_once("Var_Dump.php"); // just for kicks you can mess up this part to see some pear error handling - $dsn["username"] = 'metapear'; - $dsn["password"] = 'funky'; + $user = 'metapear'; + $pass = 'funky'; + //$pass = ""; + $host = 'localhost'; + $db_name = 'metapear_test_db'; + $db_type = "mysql"; + + // just for kicks you can mess up this part to see some pear error handling + $dsn["username"] = $user; + $dsn["password"] = $pass; //$pass = ""; - $dsn["hostspec"] = 'localhost'; + $dsn["hostspec"] = $host; // Data Source Name: This is the universal connection string - $dsn["phptype"] = "mysql"; - + $dsn["phptype"] = $db_type; + $manager = new MDB_manager; $input_file = 'metapear_test_db.schema'; - $database_variables = array( - ); -// lets create the database using 'metapear_test_db.schema' -// if you have allready run this script you should have 'metapear_test_db.schema.before' -// in that case MDb will just compare the two schemas and make any necessary modifications to the existing DB + $database_variables = array(); + // lets create the database using 'metapear_test_db.schema' + // if you have allready run this script you should have 'metapear_test_db.schema.before' + // in that case MDb will just compare the two schemas and make any necessary modifications to the existing DB $result = $manager->updateDatabase($input_file, $input_file.".before", $dsn, $database_variables); + echo "updating database from xml schema file<br>"; + - // just for kicks you can mess up this part to see some pear error handling - $user = 'metapear'; - $pass = 'funky'; - //$pass = ""; - $host = 'localhost'; - $db_name = 'metapear_test_db'; // Data Source Name: This is the universal connection string - $dsn = "mysql://$user:$pass@$host/$db_name"; + $dsn = "$db_type://$user:$pass@$host/$db_name"; // MDB::connect will return a Pear DB object on success // or a Pear DB Error object on error // You can also set to TRUE the second param // if you want a persistent connection: - // $db = MDB::connect($dsn, true); + // $db = MDB::connect($dsn, TRUE); $db = MDB::connect($dsn); // With MDB::isError you can differentiate between an error or // a valid connection. if (MDB::isError($db)) { - die ($db->getMessage()); + die ($db->getMessage()); } // happy query @@ -76,6 +82,8 @@ echo Var_Dump::display($db->tableInfo($result))."<br>"; // lets just get everything and free the result $result = $db->query($query); + $types = array('integer', 'text', 'timestamp'); + $db->setResultTypes($result, $types); $db->fetchAll($result, &$array); echo "<br>all:<br>"; echo Var_Dump::display($array)."<br>"; @@ -85,7 +93,6 @@ echo "<br>all with just one call:<br>"; echo Var_Dump::display($array)."<br>"; // run the query with the offset 1 and count 1 and get a result handler - unset($result); $result = $db->limitQuery($query, 1, 1); // lets just get everything but with an associative array and free the result $db->fetchAll($result, $array, DB_FETCHMODE_ASSOC); @@ -95,31 +102,35 @@ echo "<br>create a new seq with start 3 name real_funky_id<br>"; $err = $db->createSequence("real_funky_id",3); if (MDB::isError($err)) { - echo "<br>could not create sequence again<br>"; + echo "<br>could not create sequence again<br>"; } echo "<br>get the next id:<br>"; echo $db->nextId("real_funky_id"); echo "<br>affected rows:<br>"; echo $db->affectedRows()."<br>"; // lets try an prepare execute combo - $alldata = array( array(1, 'one', 'un'), - array(2, 'two', 'deus'), - array(3, 'three', 'trois'), - array(4, 'four', 'quatre')); - $prepared_query = $db->prepare("INSERT INTO numbers VALUES(?,?,?)"); - echo "validate prepared query:<br>".$db->ValidatePreparedQuery($prepared_query)."<br>"; + $alldata = array( + array(1, 'one', 'un'), + array(2, 'two', 'deus'), + array(3, 'three', 'trois'), + array(4, 'four', 'quatre') + ); + $prepared_query = $db->prepareQuery("INSERT INTO numbers VALUES(?,?,?)"); + echo "validate prepared query:<br>".$db->validatePreparedQuery($prepared_query)."<br>"; foreach ($alldata as $row) { - echo "running execute<br>"; - $db->execute($prepared_query, $row); + echo "running execute<br>"; + $db->execute($prepared_query, $row); } // lets try an prepare execute combo - $alldata = array( array(5, 'five', 'cinq'), - array(6, 'six', 'six'), - array(7, 'seven', 'sept'), - array(8, 'eight', 'huit')); - $prepared_query = $db->prepare("INSERT INTO numbers VALUES(?,?,?)"); - $db->executeMultiple($prepared_query, $alldata); + $alldata = array( + array(5, 'five', 'cinq'), + array(6, 'six', 'six'), + array(7, 'seven', 'sept'), + array(8, 'eight', 'huit') + ); + $prepared_query = $db->prepareQuery("INSERT INTO numbers VALUES(?,?,?)"); echo "running executeMultiple<br>"; + echo Var_Dump::display($db->executeMultiple($prepared_query, $alldata))."<br>"; $array = array(4); echo "<br>see getOne in action:<br>".$db->getOne("SELECT trans_en FROM numbers WHERE number = ?",$array)."<br>"; // You can disconnect from the database with: @@ -130,26 +141,31 @@ echo "<br>see getAll in action:<br>"; echo Var_Dump::display($db->getAll("SELECT * FROM test"))."<br>"; echo "<br>see getAssoc in action:<br>"; - echo Var_Dump::display($db->getAssoc("SELECT * FROM test", false, "", DB_FETCHMODE_ASSOC))."<br>"; + echo Var_Dump::display($db->getAssoc("SELECT * FROM test", FALSE, "", DB_FETCHMODE_ASSOC))."<br>"; echo "tableInfo on a string:<br>"; echo Var_Dump::display($db->tableInfo("numbers"))."<br>"; + echo "<br>just a simple delete query:<br>"; + echo Var_Dump::display($db->query("DELETE FROM numbers"))."<br>"; -// ok noew lets create a new xml schema file from the existing DB -// we will not use the 'metapear_test_db.schema' for this -// this feature is especially interesting for people that have an existing Db and want to move to MDB's xml schema management + + // ok now lets create a new xml schema file from the existing DB + // we will not use the 'metapear_test_db.schema' for this + // this feature is especially interesting for people that have an existing Db and want to move to MDB's xml schema management $manager->setupDatabase($dsn); $manager->getDefinitionFromDatabase(); -// this is the database definition as an array + // this is the database definition as an array echo Var_Dump::display($manager->database_definition)."<br>"; -// new we will write this array as an xml schema file + // new we will write this array as an xml schema file $manager->debug = "Output"; - echo $manager->dumpDatabase(array( - "Output" => "Dump", - "EndOfLine" => "\n", - "Output_Mode" => "file", - "Output_File" => $manager->database->database_name.'2.schema' - )); + echo $manager->dumpDatabase( + array( + "Output" => "Dump", + "EndOfLine" => "\n", + "Output_Mode" => "file", + "Output_File" => $manager->database->database_name.'2.schema' + ) + ); if($manager->database) { echo $manager->database->debugOutput(); }
« previous php.pear.cvs (#4572) next »