Guest User

Untitled

a guest
Aug 9th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. insert data into a database with prepared statements pdo
  2. $hostname = 'XXX';
  3. $database = 'XXX';
  4. $username = 'XXX';
  5. $password = 'XXX';
  6. $database = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
  7.  
  8. function get_data($url)
  9. {
  10. $ch = curl_init();
  11. $timeout = 5;
  12. curl_setopt($ch,CURLOPT_URL,$url);
  13. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  14. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  15. $data = curl_exec($ch);
  16. curl_close($ch);
  17. return $data;
  18. }
  19.  
  20. $html = get_data($_GET['url']);
  21. preg_match_all('/<font face="Arial"><strong>(.*)<br><font color/', $html, $name);
  22. preg_match('/<strong>([A-Zs0-9]+) BIRTHDAYS</strong>/', $html, $date);
  23.  
  24. for ($i=0, $n=count($name[1]); $i<$n; ++$i) {
  25. try {
  26. $insert = $database->prepare("INSERT INTO bday (name, birthdate) VALUES (:name, :date)");
  27.  
  28. $database->bindParam(':name', $name[0][$i]);
  29. $database->bindParam(':date', $date[1]);
  30.  
  31. $insert->execute();
  32. }
  33.  
  34. catch (PDOException $e) {
  35. echo $e->getMessage();
  36. }
  37. }
  38.  
  39. $content = file_get_contents('filename');
  40.  
  41. $line = explode('n', $contents);
  42.  
  43. $ct = count($arr);
  44. while($i < $ct)
  45. {
  46. $arr = explode(',', $line[$i]);
  47. /* Insert into database value $i, $arr[1], $arr[2], just ignore $arr[0] which contains your original string */
  48. $i++;
  49. }
  50.  
  51. $array = explode("n",file_get_contents("file.txt"));
  52. foreach($array as $count => $line) {
  53. $array[$count] = str_replace('xxx',$count+1,$line);
  54. }
Add Comment
Please, Sign In to add comment