Created
October 24, 2011 07:49
-
-
Save fzaninotto/1308547 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
For anyone trying to use this - bindValues should be 1,2,3,4 (not 1,2,1,1)
add a composer.json and use composer.phar to install
Then add the autoloader to the php
DB table schema that worked...
:)