Step 1: Install PHP Faker Plugin Using Composer
composer require fzaninotto/faker
Step 2: Create MySQL Database and Tables:
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(75) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(75) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Step 3: Import Faker Object and Connect Database to generate Dummy Data:
<?php
/**
* Populate MySQL Table Using faker
*
* @author
*/
require_once('./vendor/autoload.php');
try{
$count = 100000;
$faker = \Faker\Factory::create();
//Connecting MySQL Database
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'mysql', array(
PDO::ATTR_PERSISTENT => true
));
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//Drop the table
$stmt = $pdo->prepare("truncate table users");
$stmt->execute();
//Insert the data
$sql = 'INSERT INTO users (first_name, last_name, email, created)
VALUES (:first_name, :last_name, :email, :created)';
$stmt = $pdo->prepare($sql);
for ($i=0; $i < $count; $i++) {
$date = $faker->dateTime($max = 'now', 'UTC')->format('Y-m-d H:i:s');
$stmt->execute(
[
':first_name' => $faker->firstName,
':last_name' => $faker->lastName,
':email' => $faker->email,
':created' => $date
]
);
}
} catch(Exception $e){
echo '<pre>';print_r($e);echo '</pre>';exit;
}