-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example/insert-or-update-user-for-mysql.php
- Loading branch information
Showing
2 changed files
with
23 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; // load composer | ||
|
||
use Goodby\CSV\Import\Standard\Lexer; | ||
use Goodby\CSV\Import\Standard\Interpreter; | ||
use Goodby\CSV\Import\Standard\LexerConfig; | ||
|
||
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root'); | ||
$pdo->query('CREATE TABLE IF NOT EXISTS user2 (id INT, `name` VARCHAR(255), email VARCHAR(255), PRIMARY KEY (`id`))'); | ||
|
||
$config = new LexerConfig(); | ||
$lexer = new Lexer($config); | ||
|
||
$interpreter = new Interpreter(); | ||
|
||
$interpreter->addObserver(function(array $columns) use ($pdo) { | ||
$stmt = $pdo->prepare('REPLACE user2 (id, name, email) VALUES (?, ?, ?)'); | ||
$stmt->execute($columns); | ||
}); | ||
|
||
$lexer->parse('user.csv', $interpreter); |
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