-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathsample.php
34 lines (28 loc) · 1.08 KB
/
sample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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;
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
// Importing
$config = new LexerConfig();
$lexer = new Lexer($config);
$interpreter = new Interpreter();
$interpreter->addObserver(function(array $columns) use ($pdo) {
$stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
$stmt->execute($columns);
});
$lexer->parse('user.csv', $interpreter);
// Exporting
$config = new ExporterConfig();
$exporter = new Exporter($config);
$exporter->export('php://output', array(
array('1', 'alice', '[email protected]'),
array('2', 'bob', '[email protected]'),
array('3', 'carol', '[email protected]'),
));