-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_import.php
31 lines (28 loc) · 1.02 KB
/
data_import.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
<?php
// Get access to MySQL Class
require_once(dirname(__FILE__).'/MySQL.class.php');
// Get MySQL connection
$conn = new MySQL('127.0.0.1','peoplecolour','root','ilovetocode');
// Contents of file - should be better than array i presume and less memory?
$handle = fopen("users.csv", "r");
if ($handle) {
//read file line by line
while (($line = fgets($handle)) !== false) {
// set the column headings
$table_field = array('email','lastname','firstname','fav_colours','dob');
// Will nicely get data correctly and encapsulate the strings in quotes
$values = str_getcsv($line);
// To make the DOB a european date - should have data input validation if doing this
$values[4] = str_replace('/', "-", $values[4]);
$time = strtotime($values[4]);
// Get the date into the correct format for insert to MySQL
$values[4] = date('Y-m-d',$time);
// Insert the data
$conn->insert('people',$table_field,$values);
}
} else {
// error opening the file.
die('error opening file');
}
fclose($handle);
?>