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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
<?php
/**
* Oracle 8i database connection/querying layer
*
* ALPHA VERSION - not debugged!!
*
* SourceForge: Breaking Down the Barriers to Open Source Development
* Copyright 1999-2001 (c) VA Linux Systems
* http://sourceforge.net
*
* @version $Id: database-oci8.php 3442 2004-10-08 21:39:22Z gsmet $
*/
/**
* An array of an array of associative arrays
* containing row data from queries (3D array)
*
* Say that 50 times backwards while standing on your head and you
* get a free SourceForge cookie!
*
* @var array $sys_db_oci_commit_mode
*/
$sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
/**
* db_connect() - Connect to the database
*
* Notice the global vars that must be set up
* Sets up a global $conn variable which is used
* in other functions in this library
*/
function db_connect() {
global $sys_dbhost,$sys_dbuser,$sys_dbpasswd,$conn,$sys_dbname;
$conn = @ocilogon($sys_dbuser,$sys_dbpasswd,$sys_dbname);
#return $conn;
}
/**
* db_query() - Query the database
*
* NOTE - the OCI version of this may be somewhat inefficient
* for large result sets (hundreds or thousands of rows selected)
* However - most queries are returning 25-50 rows
*
* @param string SQL statement
* @param int How many rows do you want returned
* @param int Of matching rows, return only rows starting here
*/
function db_query($qstring,$limit='-1',$offset=0) {
global $QUERY_COUNT,$sys_db_results,$sys_db_row_pointer,$sys_db_oci_commit_mode;
$QUERY_COUNT++;
$stmt=@ociparse($conn,$qstring);
if (!$stmt) {
return 0;
} else {
if ($limit > 0) {
if (!$offset || $offset < 0) {
$offset=0;
}
}
$res=@ociexecute($stmt,$sys_db_oci_commit_mode);
if (!$res) {
return 0;
} else {
//if offset, seek to starting point
//potentially expensive if large offset
//however there is no data_seek feature AFAICT
$more_data=true;
if ($offset > 0) {
for ($i=0; $i<$offset; $i++) {
//burn them off
@ocifetchinto($res,$x);
if (!$x[1]) {
//if no data be returned
//get out of loop
$more_data=false;
break;
}
}
}
$i=0;
while ($more_data) {
$i++;
@ocifetchinto($res,$x,'OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS');
$sys_db_results[$res][$i-1]=$x;
//see if data is being returned && we are
//still within the requested $limit
if (count($x) < 1 || (($limit > 0) && ($i >= $limit))) {
$more_data=false;
}
}
$sys_db_row_pointer[$res]=0;
return $res;
}
}
}
/**
* db_begin() - Begin a transaction
*/
function db_begin() {
global $sys_db_oci_commit_mode;
$sys_db_oci_commit_mode='OCI_DEFAULT';
}
/**
* db_commit() - Commit a transaction
*/
function db_commit() {
global $sys_db_oci_commit_mode,$conn;
$sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
return ocicommit($conn);
}
/**
* db_rollback() - Rollback a transaction
*/
function db_rollback() {
global $sys_db_oci_commit_mode,$conn;
$sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
return ocirollback($conn);
}
/**
* db_numrows() - Returns the number of rows in this result set
*
* @param string Query result set handle
*/
function db_numrows($qhandle) {
global $sys_db_results;
// return only if qhandle exists, otherwise 0
if ($qhandle) {
return @count($sys_db_results[$qhandle]);
} else {
return 0;
}
}
/**
* db_free_result() - Frees a database result properly
*
* @param string Query result set handle
*/
function db_free_result($qhandle) {
global $sys_db_results;
unset($sys_db_results[$qhandle]);
return @ocifreestatement($qhandle);
}
/**
* db_reset_result() - Reset a result set
*
* Reset is useful for db_fetch_array
* sometimes you need to start over
*
* @param string Query result set handle
* @param int Row number
*/
function db_reset_result($qhandle,$row=0) {
global $sys_db_row_pointer;
return $sys_db_row_pointer[$qhandle]=$row;
}
/**
* db_result() - Returns a field from a result set
*
* @param string Query result set handle
* @param int Row number
* @param string Field name
*/
function db_result($qhandle,$row,$field) {
global $sys_db_results;
return $sys_db_results[$qhandle][$row]["$field"];
}
/**
* db_numfields() - Returns the number of fields in this result set
*
* @param sting Query result set handle
*/
function db_numfields($lhandle) {
return @ocinumcols($lhandle);
}
/**
* db_fieldname() - Returns the number of rows changed in the last query
*
* @param string Query result set handle
* @param int Column number
*/
function db_fieldname($lhandle,$fnumber) {
return @ocicolumnname($lhandle,$fnumber);
}
/**
* db_affected_rows() - Returns the number of rows changed in the last query
*
* @param string Query result set handle
*/
function db_affected_rows($qhandle) {
return @ocirowcount($qhandle);
}
/**
* db_fetch_array() - Fetch an array
*
* Returns an associative array from
* the current row of this database result
* Use db_reset_result to seek a particular row
*
* @param string Query result set handle
*/
function db_fetch_array($qhandle) {
global $sys_db_results,$sys_db_row_pointer;
return $sys_db_results[$qhandle][$sys_db_row_pointer++];
}
/**
* db_insertid() - Returns the last primary key from an insert
*
* @param string Query result set handle
* @param string The name of the table you inserted into
* @param string The field name of the primary key
*/
function db_insertid($qhandle,$table_name,$pkey_field_name) {
$res=db_query("SELECT max($pkey_field_name) AS id FROM $table_name");
if ($res && db_numrows($res) > 0) {
return @db_result($res,0,'id');
} else {
return 0;
}
}
/**
* db_error() - Returns the last error from the database
*/
function db_error() {
global $conn;
$err= @ocierror($conn);
if ($err) {
return $err['message'];
} else {
return false;
}
}
?>
|