Advertisement
denik-od

Test MySQL SELECT-UPDATE/INSERT overload

Aug 7th, 2018
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.65 KB | None | 0 0
  1. <?php
  2.     global $DB;
  3.  
  4.     $code = rand(10, 100000);
  5.     //$code = date("YmdHis");
  6.  
  7.     $data = array(
  8.         'code' => $code,
  9.         'data' => 'test data'
  10.     );
  11.  
  12.     $count_insert = 0;
  13.     $count_update = 0;
  14.  
  15.     for( $i=0; $i < 100; $i++ )
  16.     {
  17.        
  18.         // Check of exists
  19.         $q = $DB->query("SELECT * FROM `test_overload` WHERE `code`='{$code}'");
  20.         if( $q->num_rows == 0 )
  21.         {
  22.             $DB->query($DB->insert_string('test_overload', $data));
  23.             $count_insert++;
  24.         }
  25.         else
  26.         {
  27.             $DB->query($DB->update_string('test_overload', $data, array('id'=>$q->row['id'])));
  28.             $count_update++;
  29.         }
  30.     }
  31.  
  32.     echo "<p>Inserts: $count_insert</p>";
  33.     echo "<p>Updates: $count_update</p>";
  34. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement