Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "INSANITAS";
  7.  
  8. // Create connection
  9. $conn = new mysqli($servername, $username, $password, $dbname);
  10.  
  11. // Check connection
  12. if ($conn->connect_error) {
  13.     die("Connection failed: " . $conn->connect_error);
  14. }
  15.  
  16.  
  17. //add the csv name and table name in the following two array
  18. $tables=["MOCK_DATA.csv"];
  19. $tableName=["admins"];
  20.  
  21. for($i = 0; $i < count($tables); $i++){
  22.     $r = 1;
  23.  
  24.     if(($handle = fopen($tables[$i], "r")) !== FALSE)
  25.     {
  26.         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  27.            
  28.             //skip the header
  29.             if($r == 1){
  30.                 $r++;
  31.                 continue;
  32.             }
  33.            
  34.             $num = count($data);
  35.             $query = "INSERT INTO ".$tableName[$i]." VALUES(";
  36.             for($j = 0; $j < $num - 1; $j++)
  37.                 $query = $query.'\''.$data[$i].'\''.", ";
  38.  
  39.             $query = $query.'\''.$data[$num - 1].'\''.")";
  40.            
  41.             $ret = mysqli_query($conn, $query);
  42.            
  43.             //echo "\n\n".$query."\n";
  44.             if(!$ret){
  45.                 echo "PROBLEM!!!\n\n";
  46.             }
  47.         }
  48.    
  49.         fclose($handle);
  50.     }
  51.  
  52.     else
  53.         echo "***UNABLE TO READ THE CSV : ".$tableName[i];
  54. }
  55.  
  56. mysqli_close($conn);
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement