Files
simrs-jatim/core/sqlsrv.php
2024-04-19 14:04:41 +07:00

51 lines
1.8 KiB
PHP

<?php
class SQLSRV {
protected $connection = null;
public function connect() {
// we don't need to connect twice
if ( $this->connection ) {
return;
}
// data for making connection
$mssql_server = $_ENV['FARMASI_HOST'];
$mssql_data = array("UID" => $_ENV['FARMASI_DB_USER'],
"PWD" => $_ENV['FARMASI_DB_PASS'],
"Database" => $_ENV['FARMASI_DB_NAME']);
// try to connect
$this->connection = sqlsrv_connect($mssql_server, $mssql_data);
if($this->connection){
// silent
}
else {
// log_message('error','Failed to connect with parameter[] = ['.$mssql_server.','.implode(',', $mssql_data).']');
log_message('error','Failed to connect with error = ['.print_r( sqlsrv_errors(), true).']');
return 'Failed to connect to host';
}
}
public function getData ($query) {
// reset results; is this really needed as object's variable? Can't it be just local function's variable??
$this->data_array = array();
$result = $this->query($query);
while ($row = sqlsrv_fetch_array($result)) {
$this->data_array[] = $row;
}
return $this->data_array;
}
public function query($query) {
if($this->connection == null) {
$this->connect();
}
$result = sqlsrv_query($this->connection, $query);
if(!$result) {
log_message('error','Failed query : '.$query);
}
// $result = sqlsrv_query($this->connection, $query) or die("This Query didn't work.. [QUERY = ".$query."]");
return $result;
}
}