A super easy PHP Framework for web development!
https://github.com/exacti/phacil-framework
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.0 KiB
125 lines
3.0 KiB
6 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
|
||
|
* GPLv3 General License.
|
||
|
* https://exacti.com.br
|
||
|
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
|
||
6 years ago
|
*/
|
||
|
|
||
3 years ago
|
namespace Phacil\Framework\Databases;
|
||
|
|
||
|
use \stdClass;
|
||
|
|
||
6 years ago
|
final class SQLSRV {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var resource
|
||
|
*/
|
||
6 years ago
|
private $link;
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $hostname
|
||
|
* @param string $username
|
||
|
* @param string $password
|
||
|
* @param string $database
|
||
|
* @param string $port
|
||
|
* @param string $charset
|
||
|
* @return void
|
||
|
*/
|
||
6 years ago
|
public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') {
|
||
|
/*
|
||
|
* Argument 2 passed to sqlsrv_connect() must be an array, string given
|
||
|
*/
|
||
|
$connectionInfo = array (
|
||
|
"UID" => $username,
|
||
|
"PWD" => $password,
|
||
|
"Database" => $database
|
||
|
);
|
||
|
|
||
|
if (!$this->link = \sqlsrv_connect($hostname, $connectionInfo)) {
|
||
|
exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
if (!mssql_select_db($database, $this->link)) {
|
||
|
exit('Error: Could not connect to database ' . $database);
|
||
|
}
|
||
|
*/
|
||
|
|
||
4 years ago
|
sqlsrv_query($this->link,"SET NAMES 'utf8'");
|
||
|
sqlsrv_query($this->link, "SET CHARACTER SET utf8");
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $sql
|
||
|
* @return stdClass|true
|
||
|
*/
|
||
6 years ago
|
public function query($sql) {
|
||
4 years ago
|
$resource = \sqlsrv_query($this->link, $sql);
|
||
6 years ago
|
|
||
|
if ($resource) {
|
||
|
if (is_resource($resource)) {
|
||
|
$i = 0;
|
||
|
|
||
|
$data = array();
|
||
|
|
||
3 years ago
|
while ($result = \sqlsrv_fetch_array($resource, \SQLSRV_FETCH_ASSOC)) {
|
||
6 years ago
|
$data[$i] = $result;
|
||
|
|
||
|
$i++;
|
||
|
}
|
||
|
|
||
|
\sqlsrv_free_stmt($resource);
|
||
|
|
||
|
$query = new \stdClass();
|
||
|
$query->row = isset($data[0]) ? $data[0] : array();
|
||
|
$query->rows = $data;
|
||
|
$query->num_rows = $i;
|
||
|
|
||
|
unset($data);
|
||
|
|
||
|
return $query;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
} else {
|
||
|
trigger_error('Error: <br />' . $sql);
|
||
|
exit();
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $value
|
||
|
* @return string
|
||
|
*/
|
||
6 years ago
|
public function escape($value) {
|
||
|
$unpacked = unpack('H*hex', $value);
|
||
|
|
||
|
return '0x' . $unpacked['hex'];
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return int */
|
||
6 years ago
|
public function countAffected() {
|
||
|
return \sqlsrv_rows_affected($this->link);
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return false|int */
|
||
6 years ago
|
public function getLastId() {
|
||
|
$last_id = false;
|
||
|
|
||
4 years ago
|
$resource = \sqlsrv_query($this->link, "SELECT @@identity AS id");
|
||
6 years ago
|
|
||
|
if ($row = \sqlsrv_fetch($resource)) {
|
||
|
$last_id = trim($row[0]);
|
||
|
}
|
||
|
|
||
|
sqlsrv_free_stmt($resource);
|
||
|
|
||
|
return $last_id;
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return void */
|
||
6 years ago
|
public function __destruct() {
|
||
|
\sqlsrv_close($this->link);
|
||
|
}
|
||
|
}
|