Generate Unlimited Dummy Data(Text & Images)

Posted on

Have you ever found yourself wanting to generate thousands of records in a very short time on multiple servers but never found a way of doing it?.

Well,maybe not.

There are are numerous solutions online but all they give you is a text file containing the data which you then dump into your database.If you then want to create some data on two remote servers,you will be forced to login to the server and dump the data yourself.

I made a small script that does the hard work for you.

  1. Create a table i.e employee

    create table employee(
       emp_id INT NOT NULL AUTO_INCREMENT,
       emp_name VARCHAR(100) NOT NULL,
       emp_salary VARCHAR(100) NOT NULL,
       PRIMARY KEY ( emp_id )
    );

2. Create a php file i.e dg.php

    ```php

    <?php
    while(true){
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    if(!function_exists('generateRandStr')){
    function generateRandStr($length){ 
          $randstr = ""; 
          for($i=0; $i<$length; $i++){ 
             $randnum = mt_rand(0,61); 
             if($randnum < 10){ 
                $randstr .= chr($randnum+48); 
             }else if($randnum < 36){ 
                $randstr .= chr($randnum+55); 
             }else{ 
                $randstr .= chr($randnum+61); 
             } 
          } 
          return $randstr; 
       } 
    }
    ob_start();
    $im = @imagecreate(120, 120)
        or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($im, rand(), rand(), rand());
    $text_color = imagecolorallocate($im, rand(2,2), rand(9,5), rand(3,3));
    imagestring($im, rand(4,4), rand(4,4), rand(4,4),  preg_replace('/([ ])/e', 'chr(rand(97,122))', '     ')
    , $text_color);
    $theimage = imagepng($im);
    $image_data = ob_get_contents();
    ob_end_clean();
    $image_data_base64 = base64_encode($image_data);
    $image =  $image_data_base64;


    $emp_name = generateRandStr(30) ;
    $emp_salary = generateRandStr(30);

    $sql = "INSERT INTO employee ".
           "(emp_name,emp_salary) ".
           "VALUES ".
           "('$emp_name','$emp_salary')";
    mysql_select_db('test');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not enter data: ' . mysql_error());
    }
    echo rand()."--- Entered data successfullyn";
    sleep(1);
    }

    ?>

Don’t leave this running all night.Bad things might happen.

To run the script,use php cli

php dg.php

Use Control + Pause To Exit

Leave a Reply

Your email address will not be published.