Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2. //1. Create a database connection
  3.  
  4. require_once('config.php');
  5. $mysql_host = DB_HOST;
  6. $mysql_database = DB_NAME;
  7. $mysql_username = DB_USER;
  8. $mysql_password = DB_PASS;
  9.  
  10. try {
  11. $db = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
  12. // set the PDO error mode to exception
  13. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. $db->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
  15.  
  16. } catch (Exception $e) {
  17. die("Unable to connect: " . $e->getMessage());
  18. }
  19.  
  20. $UPStartDate= $_POST["start_date"];
  21. $UPEndDate= $_POST["start_end"];
  22.  
  23. try {
  24. // Return errors
  25. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26.  
  27. // Begin transaction
  28. $db->beginTransaction();
  29.  
  30. // Query 1
  31. $stm = $db->prepare("SELECT
  32. table.type AS ColumnA,
  33. table.cost AS ColumnB,
  34. table.time AS ColumnC
  35.  
  36. FROM
  37. table
  38.  
  39. WHERE table.recorded BETWEEN :UPStartDate AND :UPEndDate");
  40.  
  41. $stm->bindParam(':UPStartDate', $UPStartDate, PDO::PARAM_STR);
  42. $stm->bindParam(':UPEndDate', $UPEndDate, PDO::PARAM_STR);
  43. $stm->execute();
  44.  
  45. $field = $stm->fetchAll();
  46.  
  47. foreach ($field as $row) {
  48. print $row["ColumnA"] . "|" .$row["ColumnB"] . "|" .$row["ColumnC"] ."n";
  49. }
  50.  
  51.  
  52. $db->commit();
  53. $db = null; // Disconnect
  54. } catch (Exception $e) {
  55. // If transaction fail, use checkpoint and rollback
  56. $db->rollBack();
  57. echo "Update failed: " . $e->getMessage().'<br />';
  58. file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
  59. }
  60.  
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement