Guest User

Untitled

a guest
Aug 23rd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4. * File: insertPersonData.php
  5. * By: TMIT
  6. * Date: 20100601
  7. *
  8. * This script sets up an ARRAY for tPerson field names
  9. * and an ARRAY for 3 tPerson ROWs of data
  10. *
  11. * the script creates an SQL INSERT statement to insert these rows into tPerson
  12. *
  13. * We then modify the script to
  14. * a) use a 'while () {}' construct to create the INSERT statement
  15. * irrespective of how many ROWS of data existed
  16. * b) use php: fopen, feof, fgets and fclose to read the data
  17. * from a file instead of inside the script
  18. * c) use the php 'explode' construct to set a variable to
  19. * an array comprising a the line of data in the datafile
  20. *
  21. *=====================================
  22. */
  23.  
  24. { // Connect and Test MySQL and specific DB (return $dbSuccess = T/F)
  25.  
  26. $hostname = "localhost";
  27. $username = "root";
  28. $password = "";
  29. $databaseName = "alphacrm";
  30.  
  31. $dbConnected = @mysql_connect($hostname, $username, $password);
  32. $dbSelected = @mysql_select_db($databaseName,$dbConnected);
  33.  
  34. $dbSuccess = true;
  35. if ($dbConnected) {
  36. if (!$dbSelected) {
  37. echo "DB connection FAILED<br /><br />";
  38. $dbSuccess = false;
  39. }
  40. } else {
  41. echo "MySQL connection FAILED<br /><br />";
  42. $dbSuccess = false;
  43. }
  44. }
  45.  
  46. // Execute code ONLY if connections were successful
  47. if ($dbSuccess) {
  48.  
  49. { // setup ARRAY of field names
  50. $personField = array(
  51. 'Salutation' => 'Salutation',
  52. 'FirstName' => 'FirstName',
  53. 'LastName' => 'LastName',
  54. 'CompanyID' => 'CompanyID',
  55. );
  56. }
  57.  
  58. { // setup ARRAY of data ROWS
  59.  
  60.  
  61. // read CSV data file
  62.  
  63. $file = fopen("datafile", "r"); // open the file 'datafile' for 'r'eading
  64. $i = 0;
  65. while(!feof($file)) // while NOT the End Of File
  66. {
  67. $thisLine = fgets($file); // gets the next line from 'datafile'
  68. $personData[$i] = explode(",", $thisLine);// sets
  69. // $personData[$i] = array( $thisLine );
  70. // whatever's in $thisline separated by commas .
  71. $i++; // increment $i
  72. }
  73. fclose($file); // close the file
  74.  
  75. $numRows = sizeof($personData);
  76.  
  77.  
  78. }
  79.  
  80.  
  81. { // SQL statement with ARRAYS
  82.  
  83. // Fieldnames part of INSERT statement
  84. $person_SQLinsert = "INSERT INTO tPerson (
  85. ".$personField['Salutation'].",
  86. ".$personField['FirstName'].",
  87. ".$personField['LastName'].",
  88. ".$personField['CompanyID']."
  89. ) ";
  90.  
  91. // VALUES part of INSERT statement
  92. $person_SQLinsert .= "VALUES ";
  93.  
  94.  
  95.  
  96. $indx = 0;
  97. while($indx < $numRows) {
  98. $person_SQLinsert .= "(
  99. '".$personData[$indx][0]."',
  100. '".$personData[$indx][1]."',
  101. '".$personData[$indx][2]."',
  102. '".$personData[$indx][3]."'
  103. ) ";
  104. if ($indx < ($numRows - 1)) {
  105. $person_SQLinsert .= ",";
  106. }
  107.  
  108. $indx++;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. }
  115.  
  116.  
  117.  
  118. { // Echo and Execute the SQL and test for success
  119.  
  120. echo "<strong><u>SQL:<br /></u></strong>";
  121. echo $person_SQLinsert."<br /><br />";
  122.  
  123. if (mysql_query($person_SQLinsert)) {
  124. echo "was SUCCESSFUL.<br /><br />";
  125. } else {
  126. echo "FAILED.<br /><br />";
  127. }
  128.  
  129. }
  130.  
  131. } // END ($dbSuccess)
  132.  
  133. ?>
Add Comment
Please, Sign In to add comment