-
-
Save BaskSerg/508149490551b5e91ab820f505ab50f1 to your computer and use it in GitHub Desktop.
Inserting data to a database using Faker and PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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