-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLSRV.php
More file actions
235 lines (200 loc) · 5.82 KB
/
SQLSRV.php
File metadata and controls
235 lines (200 loc) · 5.82 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
* Microsoft SQL Server Driver for PHP
*/
class SQLSRV{
private $settings = array(
'Host' => 'HOSTNAME',
'Database' => 'DATABASE',
'UID' => 'USERNAME',
'PWD' => 'PASSWORD',
"CharacterSet" => "UTF-8",
"ConnectionPooling" => "1",
"MultipleActiveResultSets" => '0'
);
private $connection = NULL;
private $debbug = true;
private $stmt = false;
private $transaction = false;
private $num_rows = false;
// Connect to database
function __construct($setting = array()){
$this->settings = array_merge($this->settings, $setting);
$host = $this->settings['Host'];
unset($this->settings['Host']);
if( $this->connection = sqlsrv_connect($host, $this->settings) ) {
$this->settings = NULL;
}else{
echo "Connection could not be established.<br />";
$this->error();
}
}
// Prepares and executes a query
public function query($sql_query=false, $conn=false, $params=array(), $options=array()){
if($sql_query===false) return $this->stmt;
if(!$conn) $conn = $this->connection;
$this->stmt = sqlsrv_query($conn, $sql_query, $params, $options);
if($this->debbug) $this->check_stmt();
$this->num_rows = sqlsrv_num_rows($this->stmt);
return $this;
}
// Prepares a query for execution
public function prepare($sql_query=false, $params=array(), $conn=false){
if($sql_query===false) return $this->stmt;
if(!$conn) $conn = $this->connection;
$this->stmt = sqlsrv_prepare($conn, $sql_query, $params);
if($this->debbug) $this->check_stmt();
$this->num_rows = sqlsrv_num_rows($this->stmt);
return $this;
}
// Executes a statement prepared with $this->prepare()
public function execute(){
if($this->debbug && sqlsrv_execute( $this->stmt ) === false)
{
$this->error();
}
return $this;
}
// Returns the number of rows modified by the last INSERT, UPDATE, or DELETE query executed
public function rows_affected(){
$rows_affected = sqlsrv_rows_affected($this->stmt);
if($rows_affected === false && $this->debbug) {
$this->error();
}
return $rows_affected;
}
// Retrieves metadata for the fields
public function field_metadata(){
$array = array();
$sqlsrv_field_metadata = sqlsrv_field_metadata( $this->stmt );
foreach($sqlsrv_field_metadata as $row => $fieldMetadata ) {
$array[]=$fieldMetadata["Name"];
}
return $array;
}
// Makes the next row in a result set available for reading
public function fetch(){
if(sqlsrv_fetch( $this->stmt ) === false && $this->debbug) {
$this->error();
}
return $this;
}
// Gets field data from the currently selected row
public function get_field($index, $getAsType=NULL){
return sqlsrv_get_field( $this->stmt, $index, $getAsType );
}
// Returns a rows as an array
public function fetch_array(){
$array = array();
while( $row = sqlsrv_fetch_array($this->stmt, SQLSRV_FETCH_ASSOC) ) {
$array[]=$row;
}
return $array;
}
// Retrieves the rows of data in a result set as an object
public function fetch_object(){
$array = array();
while( $row = sqlsrv_fetch_object($this->stmt) ) {
$array[]=$row;
}
return $array;
}
// Retrieves the number of rows in a result set
public function num_rows(){
return $this->num_rows;
}
// Begins a database transaction
public function begin_transaction($conn=false){
if(!$conn) $conn = $this->connection;
if($this->debbug)
{
$this->transaction = false;
if ( sqlsrv_begin_transaction( $conn ) === false ) {
$this->error();
}
$this->transaction = true;
}
else
{
$this->transaction = sqlsrv_begin_transaction( $conn );
}
return $this;
}
// Commits a transaction that was begun with $this->begin_transaction()
public function commit($conn=false){
if(!$conn) $conn = $this->connection;
$this->transaction = sqlsrv_commit( $conn );
return $this;
}
// Rolls back a transaction that was begun with $this->begin_transaction()
public function rollback($conn=false){
if(!$conn) $conn = $this->connection;
$this->transaction = sqlsrv_rollback( $conn );
return $this;
}
// Return transaction status true/false
public function transaction(){
return $this->transaction;
}
// Sends data from parameter streams to the server
public function send_stream_data(){
$i = 1;
while(sqlsrv_send_stream_data( $this->stmt )) ++$i;
return $i;
}
// Returns error and warning information about the last SQLSRV operation performed
public function error(){
die( '<pre>' . print_r( sqlsrv_errors(), true) . '</pre>' );
}
// Check resources for the specified statement
public function check_stmt(){
if($this->stmt === false) {
$this->error();
}
return $this;
}
// Get resources for the specified statement
public function get_stmt(){
return $this->stmt;
}
// Frees all resources for the specified statement
public function free_stmt(){
return sqlsrv_free_stmt( $this->stmt );
}
// Disable instance
public function disable_debbug(){
$this->debbug = false;
return $this;
}
// Cancels a statement
public function cancel(){
sqlsrv_cancel( $this->stmt );
return $this;
}
// Returns information about the client and specified connection
public function client_info(){
return sqlsrv_client_info( $this->connection );
}
// Returns information about the server
public function server_info(){
return sqlsrv_server_info( $this->connection );
}
// Closes an open connection and releases resourses associated with the connection
public function close($conn=false){
if($conn) return sqlsrv_close( $conn );
$conn = $this->connection;
$this->stmt = false;
$this->settings = NULL;
$this->transaction = false;
$this->debbug = true;
return sqlsrv_close( $conn );
}
// Creset all on script end
function __destruct(){
$this->stmt = false;
$this->settings = NULL;
$this->transaction = false;
$this->debbug = true;
return $this->close( $this->connection );
}
}