ncurran217 Posted August 20, 2013 Share Posted August 20, 2013 I have this import code and it works if I take the header out of the csv file, but I would like to keep the header in the file so I can keep a template for my co-workers to use so there is no issues with what columns are which. Here is my code: <?php /* Format the errors and die */ function get_last_error() { $retErrors = sqlsrv_errors(SQLSRV_ERR_ALL); $errorMessage = 'No errors found'; if ($retErrors != null) { $errorMessage = ''; foreach ($retErrors as $arrError) { $errorMessage .= "SQLState: ".$arrError['SQLSTATE']."<br>\n"; $errorMessage .= "Error Code: ".$arrError['code']."<br>\n"; $errorMessage .= "Message: ".$arrError['message']."<br>\n"; } } die ($errorMessage); } /* connect */ 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); include '/includes/db_connect.php'; if ($connection === FALSE) { get_last_error(); } return $connection; } function query($connection, $query) { $result = sqlsrv_query($connection, $query); if ($result === FALSE) { get_last_error(); } return $result; } /* Prepare a reusable query (prepare/execute) */ function prepare ( $connection, $query, $params ) { $result = sqlsrv_prepare($connection, $query, $params); if ($result === FALSE) { get_last_error(); } return $result; } /* do the deed. once prepared, execute can be called multiple times getting different values from the variable references. */ function execute ( $stmt ) { $result = sqlsrv_execute($stmt); if ($result === FALSE) { get_last_error(); } return $result; } function fetch_array($query) { $result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); if ($result === FALSE) { get_last_error(); } return $result; } $connection = connect(); /* prepare the statement */ $query = "INSERT Records values ( ? , ? , ? )"; $param1 = null; // this will hold col1 from the CSV $param2 = null; // this will hold col2 from the CSV $param3 = null; // this will hold col3 from the CSV $params = array( &$param1, &$param2, &$param3 ); $prep = prepare ( $connection, $query, $params ); // $result = execute ( $prep ); //get the csv file $file = $_FILES["importrecords"]["tmp_name"]; /* Here is where you read in and parse your CSV file into an array. That may get too large, so you would have to read smaller chunks of rows. */ $csv_array = file($file); $_SESSION['records_row_count'] = 0; foreach ($csv_array as $row_num => $row) { $_SESSION['records_row_count']++; $row = trim ($row); $column = explode ( ',' , $row ); $param1 = $column[0]; $param2 = $column[1]; $param3 = $column[2]; // insert the row $result = execute ( $prep ); } /* Free statement and connection resources. */ sqlsrv_close($connection); header( "Location: import.php?successrecords=1" ); ?> How would I accomplish leaving the header in the csv file but skipping on the import? I tried adding a count, but that didn't work. Thanks for the help in advance! Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 20, 2013 Share Posted August 20, 2013 After this: $csv_array = file($file); Do This: unset($csv_array[0]); Quote Link to comment Share on other sites More sharing options...
ncurran217 Posted August 20, 2013 Author Share Posted August 20, 2013 Awesome thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.