Source for file new_insert.php

Documentation is available at new_insert.php

  1. #!/usr/local/bin/php
  2. <?php
  3. /**
  4.  * Examples how to use the CreateNewInsert() method.
  5.  * @author Sascha 'SieGeL' Pfalz <php@saschapfalz.de>
  6.  * @package db_MySQL
  7.  * @subpackage Examples
  8.  * @version 0.36 (21-Sep-2008)
  9.  *  $Id: new_insert.php,v 1.2 2009/10/03 20:20:49 siegel Exp $
  10.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  11.  * @filesource
  12.  */
  13. /**
  14.  */
  15. require('functions.inc.php');
  16. $db new db_MySQL('./dbdefs.inc.php');
  17. $d WhichBR();
  18. if($d['SAPI'!= 'cli')
  19.   {
  20.   echo("<pre>\n");
  21.   }
  22.  
  23. /*
  24.  * Table for this test (must exist on the target db!!!!)
  25.  */
  26.  
  27. $table=<<<EOM
  28. CREATE TABLE IF NOT EXISTS mysqldb_new_insert_test
  29.   (
  30.   id      INTEGER NOT NULL AUTO_INCREMENT,
  31.   field1  VARCHAR(50),
  32.   field2  VARCHAR(50),
  33.   PRIMARY KEY(id)
  34.   )
  35. EOM;
  36.  
  37. $testdata array();
  38.  
  39. $testdata[1]['field1']  'wert1/1';
  40. $testdata[1]['field2']  'wert1/2';
  41.  
  42. $testdata[3]['field1']  'wert3/1';
  43. $testdata[3]['field2']  'wert3/2';
  44.  
  45. $testdata[5]['field1']  'wert5/1';
  46. $testdata[5]['field2']  'wert5/2';
  47.  
  48. $testdata[6]['field1']  'wert6/1';
  49. $testdata[6]['field2']  'wert6/2';
  50.  
  51.  
  52. echo($d['LF']."CreateNewInsert() test".$d['LF']);
  53. echo($d['HR']);
  54.  
  55. // First connect to the database:
  56.  
  57. $db->Connect();
  58.  
  59. // Check if we need to create the table:
  60. printf("Creating test table.%s%s",$d['LF'],$d['LF']);
  61. $db->Query($table);
  62.  
  63. printf("Dump of testdata used to insert into \"mysqldb_new_insert_test\"%s%s",$d['LF'],$d['LF']);
  64.  
  65. print_r($testdata);
  66. printf("Now calling PerformNewInsert() on these data...%s%s",$d['LF'],$d['LF']);
  67.  
  68. $retcode $db->PerformNewInsert('mysqldb_new_insert_test',$testdata);
  69. if(!is_array($retcode))
  70.   {
  71.   $errmsg $db->GetErrorText();
  72.   $db->Disconnect();
  73.   printf("DB-ERROR DURING INSERT: %s%s%s",$errmsg,$d['LF'],$d['LF']);
  74.   exit;
  75.   }
  76. printf("Used %d INSERT statement(s) with a total size of %u bytes for %d entries.%s",$retcode[0],$retcode[1],count($testdata),$d['LF']);
  77. echo($d['HR']);
  78.  
  79. // Now read out the data to validate input:
  80.  
  81. printf("Reading table data:%s%s",$d['LF'],$d['LF']);
  82. printf("field1     | field2%s",$d['LF']);
  83. printf("-----------+------------------%s",$d['LF']);
  84. $query 'SELECT field1, field2 FROM mysqldb_new_insert_test';
  85. $stmt $db->QueryResult($query);
  86. while($data $db->FetchResult($stmt))
  87.   {
  88.   printf("%10s | %s%s",$data['field1'],$data['field2'],$d['LF']);
  89.   }
  90. $db->FreeResult($stmt);
  91. printf("%sFinally dropping test table \"mysqldb_new_insert_test\"%s%s",$d['LF'],$d['LF'],$d['LF']);
  92. $db->Query('DROP TABLE mysqldb_new_insert_test');
  93.  
  94. // Print out footer and disconnect from database.
  95. DBFooter($d['LF'],$db);
  96.  
  97. $db->Disconnect();
  98. if($d['SAPI'!= 'cli')
  99.   {
  100.   echo("</pre>\n");
  101.   }
  102. ?>

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