Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //1. Create a database connection
- require_once('config.php');
- $mysql_host = DB_HOST;
- $mysql_database = DB_NAME;
- $mysql_username = DB_USER;
- $mysql_password = DB_PASS;
- try {
- $db = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
- // set the PDO error mode to exception
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $db->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
- } catch (Exception $e) {
- die("Unable to connect: " . $e->getMessage());
- }
- $UPStartDate= $_POST["start_date"];
- $UPEndDate= $_POST["start_end"];
- try {
- // Return errors
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // Begin transaction
- $db->beginTransaction();
- // Query 1
- $stm = $db->prepare("SELECT
- table.type AS ColumnA,
- table.cost AS ColumnB,
- table.time AS ColumnC
- FROM
- table
- WHERE table.recorded BETWEEN :UPStartDate AND :UPEndDate");
- $stm->bindParam(':UPStartDate', $UPStartDate, PDO::PARAM_STR);
- $stm->bindParam(':UPEndDate', $UPEndDate, PDO::PARAM_STR);
- $stm->execute();
- $field = $stm->fetchAll();
- foreach ($field as $row) {
- print $row["ColumnA"] . "|" .$row["ColumnB"] . "|" .$row["ColumnC"] ."n";
- }
- $db->commit();
- $db = null; // Disconnect
- } catch (Exception $e) {
- // If transaction fail, use checkpoint and rollback
- $db->rollBack();
- echo "Update failed: " . $e->getMessage().'<br />';
- file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement