use prepared statements and throw error

This commit is contained in:
2023-05-23 15:19:47 -04:00
parent f08d7a2d6c
commit ae968a6b3e

View File

@@ -1,10 +1,14 @@
<?php <?php
/** /**
* @desc A simple and convenient php sqlsrv class * @desc A simple and convenient php sqlsrv class
* @author Yaseng WwW.Yaseng.Me [Yaseng@UAUC.NET] * @author Yaseng WwW.Yaseng.Me []
* @link http://yaseng.me/sqlsrv-class.html * v1.3
* @link
*/ */
class sqlsrv{
class sqlsrv
{
var $error_log = array(); var $error_log = array();
var $sql_log = array(); var $sql_log = array();
@@ -13,53 +17,76 @@ class sqlsrv{
var $conn; var $conn;
//connection //connection
function sqlsrv($server, $user, $pass, $dbname) { function __construct($server, $user, $pass, $dbname)
$this->conn = @sqlsrv_connect($server, array('UID' => $user ,'PWD'=> $pass, 'Database' => $dbname)); {
if($this->conn === false) { $this->conn = @sqlsrv_connect($server, array('UID' => $user, 'PWD' => $pass, 'Database' => $dbname));
if ($this->conn === false) {
$this->error_log[] = sqlsrv_errors(); $this->error_log[] = sqlsrv_errors();
die(); throw new Exception($this->error_log[0][0]["message"]);
} }
} }
//query source //query source
function query($sql){ function query($sql, $params = null)
$stmt = sqlsrv_query($this->conn, $sql); {
$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"]);
} 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)) {
$data[] = $row;
}
if (isset($data)) {
return $data;
}
} }
} }
//fetch data // $DB->count(select count(*) from users)
function fetch_all($sql) { function count($sql)
$this->query($sql); {
$count = $this->fetch_one($sql);
return $count[""];
}
function affectedRows()
{
return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false;
}
//$DB->fetch_all("SELECT * FROM table WHERE name= ?", array("name"));
function fetch_all($sql, $params = null)
{
$stmt = sqlsrv_query($this->conn, $sql, $params);
if ($stmt === false) {
throw new Exception($this->error_log[0][0]["message"]);
} else {
$data = array(); $data = array();
while($row = @sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC)) { while ($row = @sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$data[] = $row; $data[] = $row;
} }
return $data; return $data;
} }
// $DB->count(select * from users)
function fetch_one($sql){
$this->query($sql);
return sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC);
}
// $DB->count(select count(*) from users)
function count($sql){
$count=$this->fetch_one($sql);
return $count[""];
} }
function affectedRows() {
return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false; // $DB->fetch_one(select * from users)
function fetch_one($sql, $params = null)
{
$stmt = sqlsrv_query($this->conn, $sql, $params);
if ($stmt === false) {
throw new Exception($this->error_log[0][0]["message"]);
} else {
return sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
}
} }
} }
?>