Last active
June 22, 2016 06:15
-
-
Save adnanfajr/7a3e80d98f2e40eee9cf6a671ecf1bef to your computer and use it in GitHub Desktop.
PHP PDO Configurations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filename : config.php | |
<?php | |
define('dbServer', '..., 1433'); | |
define('dbDatabase', '...'); | |
define('dbUser', '...'); | |
define('dbPass', '...'); | |
/** | |
* Connect to MSSQL Server and instantiate the PDO object. | |
*/ | |
try { | |
$pdo = new PDO( | |
"sqlsrv:Server=" . dbServer . ";Database=" . dbDatabase, //DSN | |
dbUser, //Username | |
dbPass //Password | |
); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} catch(PDOException $e) { | |
die('Could not connect to the database:<br/>' . $e); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'config.php'; | |
/* QUERY SELECT */ | |
$sql = "SELECT kurs FROM kurs WHERE valas = 'USD'"; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute(); | |
$pkurs = $stmt->fetch(PDO::FETCH_ASSOC); | |
$post_kurs = $pkurs['kurs']; | |
/* QUERY COUNT */ | |
$sql = "Select count(id) as isi from detail"; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute(); | |
$i = $stmt->fetch(PDO::FETCH_ASSOC); | |
$isi = $i['isi']; | |
/* QUERY INSERT */ | |
// Mulai transaksi | |
$pdo->beginTransaction(); | |
// Inisiasi semua variabel | |
$post_formno = !empty($_POST['formno']) ? trim($_POST['formno']) : null; | |
$post_date = !empty($_POST['tanggal']) ? trim($_POST['tanggal']) : date("Y-m-d"); | |
$post_prefix = !empty($_POST['prefix']) ? trim($_POST['prefix']) : null; | |
$post_valas = !empty($_POST['valas']) ? trim($_POST['valas']) : null; | |
$post_kurs = !empty($_POST['kurs']) ? trim($_POST['kurs']) : null; | |
$post_ref = !empty($_POST['refer']) ? trim($_POST['refer']) : null; | |
$post_debit = !empty($_POST['debit']) ? trim($_POST['debit']) : null; | |
$post_kredit = !empty($_POST['kredit']) ? trim($_POST['kredit']) : null; | |
// Query | |
$stmt = $pdo->prepare("INSERT INTO tran_gj ( | |
form_no, | |
dates, | |
prefix, | |
valas, | |
kurs, | |
ref, | |
debit, | |
kredit) | |
VALUES | |
(:form, :dates, :prefix, :valas, :kurs, :ref, :debit, :kredit)"); | |
// Variables | |
$stmt->bindValue(':form', $post_formno); | |
$stmt->bindValue(':dates', $post_date); | |
$stmt->bindValue(':prefix', $post_prefix); | |
$stmt->bindValue(':valas', $post_valas); | |
$stmt->bindValue(':kurs', $post_kurs); | |
$stmt->bindValue(':ref', $post_ref); | |
$stmt->bindValue(':debit', $post_debit); | |
$stmt->bindValue(':kredit', $post_kredit); | |
// Eksekusi query | |
$result = $stmt->execute(); | |
// Cek apakah berhasil | |
if($result){ | |
// Jika berhasil | |
$pdo->commit(); | |
} else { | |
// Jika gagal | |
echo '<script language="javascript">alert("Transaction rolled back!")</script>'; | |
$pdo->rollBack(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment