Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "INSANITAS";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //add the csv name and table name in the following two array
- $tables=["MOCK_DATA.csv"];
- $tableName=["admins"];
- for($i = 0; $i < count($tables); $i++){
- $r = 1;
- if(($handle = fopen($tables[$i], "r")) !== FALSE)
- {
- while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
- //skip the header
- if($r == 1){
- $r++;
- continue;
- }
- $num = count($data);
- $query = "INSERT INTO ".$tableName[$i]." VALUES(";
- for($j = 0; $j < $num - 1; $j++)
- $query = $query.'\''.$data[$i].'\''.", ";
- $query = $query.'\''.$data[$num - 1].'\''.")";
- $ret = mysqli_query($conn, $query);
- //echo "\n\n".$query."\n";
- if(!$ret){
- echo "PROBLEM!!!\n\n";
- }
- }
- fclose($handle);
- }
- else
- echo "***UNABLE TO READ THE CSV : ".$tableName[i];
- }
- mysqli_close($conn);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement