1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <?php /** * @desc A simple and convenient php sqlsrv class * @author Yaseng WwW.Yaseng.Me [Yaseng@UAUC.NET] * @link http://yaseng.me/sqlsrv-class.html */ class sqlsrv{
var $error_log = array(); var $sql_log = array(); var $query_id; var $num_rows; var $conn;
//connection function sqlsrv($server, $user, $pass, $dbname) { $this->conn = @sqlsrv_connect($server, array('UID' => $user ,'PWD'=> $pass, 'Database' => $dbname)); if($this->conn === false) { $this->error_log[] = sqlsrv_errors(); die(); } }
//query source function query($sql){ $stmt = sqlsrv_query($this->conn, $sql); $this->sql_log[] = $sql; if($stmt === false) { $this->error_log[] = sqlsrv_errors(); } else { $this->query_id = $stmt; $this->num_rows = $this->affectedRows(); } }
//fetch data function fetch_all($sql) { $this->query($sql); $data = array(); while($row = @sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC)) { $data[] = $row; } 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; } }
?>
|