Skip to content

Instantly share code, notes, and snippets.

@xtpor
Created November 30, 2017 07:23
Show Gist options
  • Save xtpor/9ac9d8278fddbe21165363a7677409db to your computer and use it in GitHub Desktop.
Save xtpor/9ac9d8278fddbe21165363a7677409db to your computer and use it in GitHub Desktop.
<?php
function exec_query($sql, $params = []) {
$username = 's1165683';
$password = '11656830';
$conn_info = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP) " .
"(HOST = oracleacademy.ouhk.edu.hk)(PORT=8998)) " .
"(CONNECT_DATA=(SERVER=DEDICATED) " .
"(SID=db1011)))";
$conn = oci_connect($username, $password, $conn_info);
if (!$conn) {
$e = oci_error();
die("Fatal: Unable to connect to the database: {$e['message']}");
}
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
die("Fatal: Failed to parse SQL statement: $sql");
}
foreach ($params as $key => &$value) {
if (!oci_bind_by_name($stmt, ":" . $key, $value)) {
die ("Fatal: Failed to bind variable $key: $sql");
}
}
if (!@oci_execute($stmt)) {
$err = oci_error($stmt);
die("Fatal: Unable to execute SQL statement: {$e['message']}");
}
$result = [];
while ($raw_row = @oci_fetch_assoc($stmt)) {
$row = [];
foreach ($raw_row as $k => $v) {
$row[strtolower($k)] = $v;
}
$result[] = $row;
}
oci_free_statement($stmt);
oci_close($conn);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment