Skip to content

Instantly share code, notes, and snippets.

@BaskSerg
Forked from fzaninotto/gist:1308547
Created January 10, 2018 07:17
Show Gist options
  • Save BaskSerg/508149490551b5e91ab820f505ab50f1 to your computer and use it in GitHub Desktop.
Save BaskSerg/508149490551b5e91ab820f505ab50f1 to your computer and use it in GitHub Desktop.
Inserting data to a database using Faker and PDO
<?php
$pdo = new PDO('mysql:host=hostname;dbname=defaultDbName', 'username', 'p@55w0rd');
$sql = 'INSERT INTO author (first_name, last_name, date_of_birth, email) VALUES (?, ?, ?, ?)';
$stmt = $pdo->prepare($sql);
$faker = \Faker\Factory::create();
$insertedPKs = array();
for ($i=0; $i < 100; $i++) {
$stmt->bindValue(1, $faker->firstName, PDO::PARAM_STR);
$stmt->bindValue(2, $faker->lastName, PDO::PARAM_STR);
$stmt->bindValue(1, $faker->date, PDO::PARAM_STR);
$stmt->bindValue(1, $faker->email, PDO::PARAM_STR);
$stmt->execute();
$insertedPKs[]= $pdo->lastInsertId();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment