PHP and SQL Server - Generating Data

In order to test the performance of a database, or application that uses it, where there are large volumes of data, it may be useful to generate data rather than having to enter it manually.

The following example demonstrates how to generate data for a table called ‘person’, which was used in the examples for selecting, inserting, updating, deletingimporting and exporting data.

First of all, a connection to the database is established and the number of records to generate is specified. This can be set to any value, as long as there is enough space in the database to accommodate the records. A number of arrays are defined so that a random first name, last name and title can be selected for each record. The first name array also holds the gender associated with the name so that an appropriate title can be selected. A date range is also specified to allow for the generation of a random date of birth, along with other variables.

A ‘for’ loop is used to generate the desired number of records. A random number is generated to select a first name from the corresponding array and this is extracted, along with the gender. The same process occurs to extract a random last name. The gender is then used to extract a title from the appropriate array. The final random value generated is the date of birth.

Once all the values have been generated, an SQL ‘insert‘ statement is constructed and then executed. Feedback is provided as to the number of records added to the database. A ‘try-catch’ block is used to catch any errors that may occur.

<?php

    // Connect to the database.
    require_once('database-connect.php');

    // Number of records to generate.
    $recordsToGenerate = 500;

    // First names.
    $fname = array
    (
        array("Oliver", "M"), array("Noah", "M"), array("Harry", "M"),
        array("Leo", "M"), array("Charlie", "M"), array("Jack", "M"),
        array("Freddie", "M"), array("Alfie", "M"), array("Archie", "M"),
        array("Theo", "M"), array("Olivia", "F"), array("Sophia", "F"),
        array("Amelia", "F"), array("Emily", "F"), array("Ava", "F"),
        array("Isla", "F"), array("Isabelle", "F"), array("Charlotte", "F"),
        array("Layla", "F"), array("Freya", "F")
    );

    // Last names.
    $lname = array
    (
        "Smith", "Johnson", "Williams", "Jones",
        "Brown", "Davis", "Miller", "Wilson",
        "Taylor", "Anderson", "Thomas", "White",
        "Martin", "Thompson", "Robinson", "Clark",
        "Walker", "Young", "Wright", "Hill"
    );

    // Male titles.
    $mtitle = array
    (
        "Mr", "Dr", "Prof"
    );

    // Female titles.
    $ftitle = array
    (
        "Miss", "Mrs", "Ms", "Dr", "Prof"
    );

    // Start and end dates for random date of birth range.
    $startDob = date('Y-m-d', strtotime('-100 year'));
    $endDob = date('Y-m-d', strtotime('-20 year'));

    // Convert the date of birth range dates to timestamps.
    // Used to generate random date of birth.
    $startDobTs = strtotime($startDob);
    $endDobTs = strtotime($endDob);

    // Record count.
    $recordCount = 0;

    try
    {

        // Generate specified number of records.
        for ($i = 1; $i <= $recordsToGenerate; $i++)
        {

            // Randomly select a first name and associated gender.
            $randomNumber = rand(0, sizeof($fname) - 1);
            $firstName = $fname[$randomNumber][0];
            $gender = $fname[$randomNumber][1];

            // Randomly select a last name.
            $randomNumber = rand(0, sizeof($fname) - 1);
            $lastName = $lname[$randomNumber];

            // Randomly select a title based on the gender.
            if ($gender == "M")
            {

                $randomNumber = rand(0, sizeof($mtitle) - 1);
                $title = $mtitle[$randomNumber];

            }
            else
            {

                $randomNumber = rand(0, sizeof($ftitle) - 1);
                $title = $ftitle[$randomNumber];

            }

            // Randomly select a date of birth.
            $randomNumber = rand($startDobTs, $endDobTs);
            $dob = date('Y-m-d', $randomNumber);

            // Prepare the query.
            $results = $connect->prepare("INSERT INTO person 
                                                 (firstname, lastname, title, dob)
                                          VALUES (?, ?, ?, ?)");
            
            // Bind the parameters.
            $results->bindParam(1, $firstName);
            $results->bindParam(2, $lastName);
            $results->bindParam(3, $title);
            $results->bindParam(4, $dob);

            // Execute the query.
            $results->execute();

            // Increment the record count.
            $recordCount += 1;

        }

        // Provide feedback on the number of records added.
        if ($recordCount == 0)
        {

            echo "No new person records added.";

        }
        else if ($recordCount == 1)
        {

            echo $recordCount . " person record added.";

        }
        else
        {

            echo $recordCount . " person records added.";

        }

    } 
    catch(Exception $e) 
    {

        // Confirm error adding person information and exit.
        echo "Error adding person information.";
        exit;

    }

?>