Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 10th, 2011  |  syntax: PHP  |  size: 2.83 KB  |  views: 138  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. $db = new mysqli( 'localhost', 'user', 'pass' );
  4. $db->select_db( 'user' );
  5. $db->set_charset( 'utf8' );
  6.  
  7. function query( $sql )
  8. {
  9.     global $db;
  10.     echo "$sql\n";
  11.     $r = mysqli_query( $db, $sql ) or die( mysqli_error($db) );
  12.     return $r;
  13. }
  14.  
  15. function cleanup()
  16. {
  17.     foreach( array(     "a_test_myisam",
  18.                     "c_test_innodb",
  19.                     "b_test",
  20.                     "c_test_myisam",
  21.                     "a_test_innodb" ) as $table )
  22.         query( "DROP TABLE IF EXISTS $table;" );
  23. }
  24.  
  25. function shutdown()
  26. {
  27.     cleanup();
  28.     exit(0);
  29. }
  30.  
  31. // start on clean db
  32. cleanup();
  33.  
  34. // destroy tables after Ctrl-C to make restoring of test backup easier
  35. declare(ticks = 1);  
  36. register_shutdown_function('shutdown');  // Catch Fatal Error (Rollback)  
  37. pcntl_signal(SIGTERM, 'shutdown');      // Catch Ctrl+C, kill and SIGTERM
  38. pcntl_signal(SIGINT, 'shutdown');
  39.  
  40. // create test tables
  41. query( "CREATE TABLE a_test_myisam (
  42.    a_id INTEGER PRIMARY KEY AUTO_INCREMENT,
  43.    x INTEGER NOT NULL DEFAULT 0
  44. ) ENGINE=MyISAM;");
  45.  
  46. query( "CREATE TABLE a_test_innodb (
  47.    a_id INTEGER PRIMARY KEY AUTO_INCREMENT,
  48.    x INTEGER NOT NULL DEFAULT 0
  49. ) ENGINE=InnoDB;");
  50.  
  51. query( "CREATE TABLE b_test (
  52.    id INTEGER PRIMARY KEY AUTO_INCREMENT,
  53.    x INTEGER NOT NULL DEFAULT 0
  54. ) ENGINE=MyISAM;");
  55.  
  56. query( "CREATE TABLE c_test_myisam (
  57.    c_id INTEGER PRIMARY KEY AUTO_INCREMENT,
  58.    x INTEGER NOT NULL DEFAULT 0
  59. ) ENGINE=MyISAM;");
  60.  
  61. query( "CREATE TABLE c_test_innodb (
  62.    c_id INTEGER PRIMARY KEY AUTO_INCREMENT,
  63.    a_id INTEGER NOT NULL,
  64.    FOREIGN KEY (a_id) REFERENCES a_test_innodb( a_id ),
  65.    KEY( a_id )
  66. ) ENGINE=InnoDB;
  67. ");
  68.  
  69. // fill b_test with some dummy rows
  70. query( "INSERT INTO b_test (x) VALUES (0);" );
  71.  
  72. for( $i=0; $i<18; $i++ )
  73.     query( "INSERT INTO b_test (x) SELECT x FROM b_test;" );
  74.  
  75. $r = mysqli_fetch_row(query( "SELECT count(*) FROM b_test" ));
  76. echo "b_test contains ${r[0]} rows\n";
  77. echo "start backup now and ctrl-c this program when backup is finished\n";
  78.  
  79. for( $i=0; $i<1000000; $i++ )
  80. {
  81.     // myisam test
  82.     mysqli_query( $db, "LOCK TABLES a_test_myisam WRITE, c_test_myisam WRITE" ) or die( mysqli_error($db) );
  83.     mysqli_query( $db, "INSERT INTO a_test_myisam (x) VALUES ($i)" ) or die( mysqli_error($db) );
  84.     mysqli_query( $db, "INSERT INTO c_test_myisam (x) VALUES ($i)" ) or die( mysqli_error($db) );
  85.     mysqli_query( $db, "UNLOCK TABLES" ) or die( mysqli_error($db) );
  86.  
  87.     // innodb test
  88.     mysqli_query( $db, "BEGIN" ) or die( mysqli_error($db) );
  89.     mysqli_query( $db, "INSERT INTO a_test_innodb (x) VALUES (0)" ) or die( mysqli_error($db) );
  90.     $id = mysqli_insert_id($db);
  91.     mysqli_query( $db, "INSERT INTO c_test_innodb (a_id) VALUES ($id)" ) or die( mysqli_error($db) );
  92.     mysqli_query( $db, "COMMIT" ) or die( mysqli_error($db) );
  93. }