preventing crash if there's no error to display.
This commit is contained in:
136
sqlsrv.php
136
sqlsrv.php
@@ -3,90 +3,90 @@
|
|||||||
/**
|
/**
|
||||||
* @desc A simple and convenient php sqlsrv class
|
* @desc A simple and convenient php sqlsrv class
|
||||||
* @author Originaly made by Yaseng WwW.Yaseng.Me, now maintained and update by Sebastien Plante
|
* @author Originaly made by Yaseng WwW.Yaseng.Me, now maintained and update by Sebastien Plante
|
||||||
* @version v1.4
|
* @version v1.5
|
||||||
* @link https://gitea.urbanghetto.net/nka/php_sqlsrv_class
|
* @link https://gitea.urbanghetto.net/nka/php_sqlsrv_class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class sqlsrv
|
class sqlsrv
|
||||||
{
|
{
|
||||||
|
|
||||||
var $error_log = array();
|
var $error_log = array();
|
||||||
var $sql_log = array();
|
var $sql_log = array();
|
||||||
var $query_id;
|
var $query_id;
|
||||||
var $num_rows;
|
var $num_rows;
|
||||||
var $conn;
|
var $conn;
|
||||||
|
|
||||||
//connection
|
//connection
|
||||||
function __construct($server, $user, $pass, $dbname)
|
function __construct($server, $user, $pass, $dbname)
|
||||||
{
|
{
|
||||||
$this->conn = @sqlsrv_connect($server, array('UID' => $user, 'PWD' => $pass, 'Database' => $dbname, 'LoginTimeout' => 10));
|
$this->conn = @sqlsrv_connect($server, array('UID' => $user, 'PWD' => $pass, 'Database' => $dbname, 'LoginTimeout' => 10));
|
||||||
if ($this->conn === false) {
|
if ($this->conn === false) {
|
||||||
$this->error_log[] = sqlsrv_errors();
|
$this->error_log[] = sqlsrv_errors();
|
||||||
throw new Exception($this->error_log[0][0]["message"]);
|
throw new Exception(($this->error_log[0][0]["message"] ?? ''));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//query source
|
//query source
|
||||||
function query($sql, $params = null)
|
function query($sql, $params = null)
|
||||||
{
|
{
|
||||||
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
||||||
$this->sql_log[] = $sql;
|
$this->sql_log[] = $sql;
|
||||||
if ($stmt === false) {
|
if ($stmt === false) {
|
||||||
$this->error_log[] = sqlsrv_errors();
|
$this->error_log[] = sqlsrv_errors();
|
||||||
throw new Exception($this->error_log[0][0]["message"]);
|
throw new Exception(($this->error_log[0][0]["message"] ?? ''));
|
||||||
} else {
|
} else {
|
||||||
$this->query_id = $stmt;
|
$this->query_id = $stmt;
|
||||||
$this->num_rows = $this->affectedRows();
|
$this->num_rows = $this->affectedRows();
|
||||||
while ($row = @sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
while ($row = @sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||||
$data[] = $row;
|
$data[] = $row;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($data)) {
|
if (isset($data)) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// $DB->count(select count(*) from users)
|
// $DB->count(select count(*) from users)
|
||||||
function count($sql)
|
function count($sql)
|
||||||
{
|
{
|
||||||
|
|
||||||
$count = $this->fetch_one($sql);
|
$count = $this->fetch_one($sql);
|
||||||
return $count[""];
|
return $count[""];
|
||||||
}
|
}
|
||||||
|
|
||||||
function affectedRows()
|
function affectedRows()
|
||||||
{
|
{
|
||||||
return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false;
|
return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//$DB->fetch_all("SELECT * FROM table WHERE name= ?", array("name"));
|
//$DB->fetch_all("SELECT * FROM table WHERE name= ?", array("name"));
|
||||||
function fetch_all($sql, $params = null)
|
function fetch_all($sql, $params = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
||||||
if ($stmt === false) {
|
if ($stmt === false) {
|
||||||
throw new Exception($this->error_log[0][0]["message"]);
|
throw new Exception(($this->error_log[0][0]["message"] ?? ''));
|
||||||
} else {
|
} else {
|
||||||
$data = array();
|
$data = array();
|
||||||
while ($row = @sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
while ($row = @sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||||
$data[] = $row;
|
$data[] = $row;
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// $DB->fetch_one(select * from users)
|
// $DB->fetch_one(select * from users)
|
||||||
function fetch_one($sql, $params = null)
|
function fetch_one($sql, $params = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
$stmt = sqlsrv_query($this->conn, $sql, $params);
|
||||||
if ($stmt === false) {
|
if ($stmt === false) {
|
||||||
throw new Exception($this->error_log[0][0]["message"]);
|
throw new Exception(($this->error_log[0][0]["message"] ?? ''));
|
||||||
} else {
|
} else {
|
||||||
return sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
return sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user