Using fgetcsv is a brand new function for me today. I'm used to dealing with mysql so sql server is also sort of a beast. Hence why I'm turning to you guys!
I have the below code which I believe does everything I need except for taking the csv, and putting it into a sql table.
Can you guys help with some sample codes that might mitigate this problem?
function connect() {
if (!function_exists('sqlsrv_num_rows')) { // Insure sqlsrv_1.1 is loaded.
die ('sqlsrv_1.1 is not available');
}
/*
* Log all Errors.
*/
sqlsrv_configure("WarningsReturnAsErrors", TRUE); // BE SURE TO NOT ERROR ON A WARNING
sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_ALL);
sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ALL);
$conn = sqlsrv_connect('instance', array
(
'UID' => 'userName',
'PWD' => 'password',
'Database' => 'database',
'CharacterSet' => 'UTF-8',
'MultipleActiveResultSets' => true,
'ConnectionPooling' => true,
'ReturnDatesAsStrings' => true,
));
if ($conn === FALSE) {
get_last_error();
}
return $conn;
}
function query($conn, $query) {
$result = sqlsrv_query($conn, $query);
if ($result === FALSE) {
get_last_error();
}
return $result;
}
function execute ( $stmt ) {
$result = sqlsrv_execute($stmt);
if ($result === FALSE) {
get_last_error();
}
return $result;
}
$conn = connect();
if (($handle = fopen(getcwd().'C:\path\TASKS.csv', "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
$db->execute("INSERT INTO Database..table (taskID, name, active) VALUES ('{$data[1]}','{$data[2]}','{$data[3]}')");
set_time_limit(60); // reset timer on loop
}
}
With the above code I am currently getting the following error
failed to open stream: Invalid argument in C:\path\insertDataFGet.php on line 51
Line 51 is the $db->execute line
Sample of csv
1,taskName1,1
2,taskName2,0
3,taskName3,0