Guest User

Untitled

a guest
Mar 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. <?php
  2.  
  3. //Define Variables
  4. $host='localhost'; //hostname of database
  5. $user='user'; //username of database
  6. $pass='pass'; //password of database
  7. $dbname='database'; //name of database
  8. $table='table'; //name of table
  9. $file='spreadsheet.csv'; //name of file to be imported (including extension)
  10.  
  11. ///////////Do not edit below this line!/////////////////////
  12.  
  13. //Connect to MySQL Database
  14. if ($dbc = @mysql_connect ($host, $user, $pass)) {
  15.  
  16. if (!@mysql_select_db ($dbname)) {
  17. die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
  18. }
  19.  
  20. } else {
  21. die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
  22. }
  23.  
  24. $fcontents = file ('./stafflist.csv');
  25. # expects the csv file to be in the same dir as this script
  26.  
  27. for($i=0; $i<sizeof($fcontents); $i++) {
  28. $line = trim($fcontents[$i]);
  29. $arr = explode(",", $line);
  30. #if your data is comma separated
  31. # instead of tab separated,
  32. # change the '\t' above to ','
  33.  
  34. $sql = "insert into $table values ('".
  35. implode("','", $arr) ."')";
  36. mysql_query($sql);
  37. echo $sql ."<br>\n";
  38. if(mysql_error()) {
  39. echo mysql_error() ."<br>\n";
  40. }
  41. }
  42. ?>
Add Comment
Please, Sign In to add comment