Source for file ddl_dml.php

Documentation is available at ddl_dml.php

  1. #!/usr/local/bin/php
  2. <?php
  3. /**
  4.  * Example shows how to do:
  5.  * - CREATE TABLE
  6.  * - INSERT
  7.  * - SELECT
  8.  * - UPDATE
  9.  * - DROP TABLE
  10.  *
  11.  * Please note that this example requires a valid login to your own
  12.  * database which allows to create/drop tables and INSERT/UPDATE data!
  13.  * The example won't run until you setup a valid login for your database!
  14.  * @author Sascha 'SieGeL' Pfalz <php@saschapfalz.de>
  15.  * @package db_MySQL
  16.  * @subpackage Examples
  17.  * @version 0.36 (29-Nov-2008)
  18.  *  $Id: ddl_dml.php,v 1.1 2008/11/29 12:33:00 siegel Exp $
  19.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  20.  * @filesource
  21.  */
  22. /**
  23.  */
  24. require('functions.inc.php');
  25. $d WhichBR();
  26. if($d['SAPI'!= 'cli')
  27.   {
  28.   echo("<pre>\n");
  29.   }
  30. echo($d['LF']);
  31.  
  32. /**
  33.  * Enter here your OWN (!) login data for your MySQL database. As long as
  34.  * you do not configure these defines this example WON'T RUN!
  35.  */
  36.  
  37. /** Database user name */
  38. define('DB_USER''siegel');
  39.  
  40. /** Database user password */
  41. define('DB_PASS''siegel');
  42.  
  43. /** Database hostname (can be empty, localhost is then assumed) */
  44. define('DB_HOST''');
  45.  
  46. /** Database TCP port number to use, defaults to 3306 */
  47. define('DB_PORT'3306);
  48.  
  49. /** Database schema name */
  50. define('DB_NAME''siegel');
  51.  
  52. if(DB_USER == '' || DB_PASS == '' || DB_NAME == '')
  53.   {
  54.   die('ERROR: Example is not configured - Please modify the defines to let this example work!'.$d['LF'].$d['LF']);
  55.   }
  56.  
  57. /*
  58.  * Create database class object and try to connect.
  59.  * Auto-error is on so if the logindata is wrong connect() won't return.
  60.  */
  61.  
  62. $db new db_MySQL;
  63.  
  64. /*
  65.  * The DDL query to create a simple table which we will fill up with random data afterwards.
  66.  */
  67.  
  68. $ddl_query[0'DROP TABLE IF EXISTS MYSQL_DB_TEST';
  69. $ddl_query[1]=<<<EOM
  70. CREATE TABLE MYSQL_DB_TEST
  71.   (
  72.   ID        INTEGER NOT NULL AUTO_INCREMENT,
  73.   TESTFIELD VARCHAR(200),
  74.   PRIMARY KEY(ID)
  75.   )
  76.  COMMENT='MySQL_db class testing table'
  77. EOM;
  78.  
  79. echo('Creating test table "MYSQL_DB_TEST" in database '.DB_NAME.'.'.$d['LF'].$d['LF']);
  80.  
  81. /*
  82.  * Now first let us create the table.
  83.  * To be on the safe side we first check if we need to drop an existing table, then we create it again.
  84.  * NOTE: We do not check anything here for errors, this is all done automatically be the class!
  85.  */
  86. for($i 0$i count($ddl_query)$i++)
  87.   {
  88.   $db->Query($ddl_query[$i]);
  89.   }
  90.  
  91. echo('Table created. Now INSERTing 10 rows with random data.'.$d['LF'].$d['LF']);
  92.  
  93. /*
  94.  * Table is now created (else class would have exited in previous loop!),
  95.  * so start filling it with 10 rows of randomized data:
  96.  */
  97.  
  98. for($i 0$i 10$i++)
  99.   {
  100.   $db->Query(sprintf("INSERT INTO MYSQL_DB_TEST(TESTFIELD) VALUES('%s')",mt_rand(0,99999999)));
  101.   }
  102.  
  103. /*
  104.  * Just for safety a COMMIT is performed, not necessary for MyISAM tables of course.
  105.  */
  106.  
  107. $db->Commit();
  108.  
  109. echo($i.' rows inserted to table, now fetching them.'.$d['LF'].$d['LF']);
  110. echo($d['HR'].$d['LF']);
  111.  
  112. /*
  113.  * Now we fetch here the data and display it to the user. We are using here the MYSQL_ASSOC result set to have
  114.  * associative arrays available. This is better readable than using numbered arrays.
  115.  */
  116.  
  117. $stmt $db->QueryResult('SELECT ID,TESTFIELD FROM MYSQL_DB_TEST ORDER BY ID');
  118. while($data $db->FetchResult($stmt))
  119.   {
  120.   printf("ID: %2d | TESTFIELD: %-30s%s",$data['ID'],$data['TESTFIELD'],$d['LF']);
  121.   }
  122. $db->FreeResult($stmt);
  123.  
  124. /*
  125.  * Now we update all data to have the same value (no WHERE clause).
  126.  * We will modify the TESTFIELD entries to have all the value '007' stored :)
  127.  */
  128.  
  129. echo($d['LF'].'Updating field "TESTFIELD" on all rows with new value "007"'.$d['LF'].$d['LF']);
  130.  
  131. $db->Query("UPDATE MYSQL_DB_TEST SET TESTFIELD='007'");
  132. $db->Commit();
  133.  
  134. /*
  135.  * Finally select again all rows and display them to validate that the update was successful.
  136.  */
  137.  
  138. echo('Now fetching again all rows to validate the update'.$d['LF']);
  139. echo($d['HR'].$d['LF']);
  140.  
  141. $stmt $db->QueryResult('SELECT ID,TESTFIELD FROM MYSQL_DB_TEST ORDER BY ID');
  142. while($data $db->FetchResult($stmt))
  143.   {
  144.   printf("ID: %2d | TESTFIELD: %-30s%s",$data['ID'],$data['TESTFIELD'],$d['LF']);
  145.   }
  146. $db->FreeResult($stmt);
  147.  
  148. /*
  149.  * Okay before ending this example we will remove our table.
  150.  * The query is still available in the variable $ddl_query[0].
  151.  */
  152.  
  153. echo($d['LF'].'Dropping test table "MYSQL_DB_TEST"'.$d['LF'].$d['LF']);
  154. $db->Query($ddl_query[0]);
  155.  
  156. /* Finally print out some stats */
  157. echo($d['LF'].$db->GetQueryCount()." queries took ".round($db->GetQueryTime(),3)." seconds.".$d['LF']);
  158. $db->Disconnect();
  159. if($d['SAPI'!= 'cli')
  160.   {
  161.   echo("</pre>\n");
  162.   }
  163. ?>

Documentation generated on Sat, 07 Aug 2010 10:18:08 +0200 by phpDocumentor 1.4.3