ADODB中几个有用的函数
By admin
- 3 minutes read - 529 words**GetOne($sql,$inputarr=false)**Executes the SQL and returns the first field of the first row. The recordset and remaining rows are discarded for you automatically. If an error occur, false is returned.**GetRow($sql,$inputarr=false)**Executes the SQL and returns the first row as an array. The recordset and remaining rows are discarded for you automatically. If an error occurs, false is returned.GetAll($sql,$inputarr=false)
Executes the SQL and returns the all the rows as a 2-dimensional array. The recordset is discarded for you automatically. If an error occurs, false is returned. GetArray is a synonym for GetAll.
GetCol($sql,$inputarr=false,$trim=false)
Executes the SQL and returns all elements of the first column as a 1-dimensional array. The recordset is discarded for you automatically. If an error occurs, false is returned.**CacheGetOne([$secs2cache,] $sql,$inputarr=false), CacheGetRow([$secs2cache,] $sql,$inputarr=false), CacheGetAll([$secs2cache,] $sql,$inputarr=false), CacheGetCol([$secs2cache,] $sql,$inputarr=false,$trim=false)*Similar to above Get functions, except that the recordset is serialized and cached in the $ADODB_CACHE_DIR directory for $secs2cache seconds. Good for speeding up queries on rarely changing data. Note that the $secs2cache parameter is optional. If omitted, we use the value in $connection->cacheSecs (default is 3600 seconds, or 1 hour).**Prepare($sql )**Prepares (compiles) an SQL query for repeated execution. Bind parameters are denoted by ?, except for the oci8 driver, which uses the traditional oracle :varname convention. Returns an array containing the original sql statement in the first array element; the remaining elements of the array are driver dependent. If there is an error, or we are emulating Prepare( ), we return the original $sql string. This is because all error-handling has been centralized in Execute( ).Prepare( ) cannot be used with functions that use SQL query rewriting techniques, e.g. PageExecute( ) and SelectLimit( ).
Example:
$stmt = $DB->Prepare('insert into table (col1,col2) values (?,?)');
for ($i=0; $i < $max; $i++)
$DB->Execute($stmt,array((string) rand(), $i));
Also see InParameter(), OutParameter() and PrepareSP() below. Only supported internally by interbase, oci8 and selected ODBC-based drivers, otherwise it is emulated. There is no performance advantage to using Prepare() with emulation. Important: Due to limitations or bugs in PHP, if you are getting errors when you using prepared queries, try setting $ADODB_COUNTRECS = false before preparing. This behaviour has been observed with ODBC. **IfNull($field, $nullReplacementValue)**Portable IFNULL function (NVL in oracle). Returns a string that represents the function that checks whether a $field is null for the given database, and if null, change the value returned to $nullReplacementValue. Eg.
$sql = 'Select '.$db->IfNull('name', "'- unknown -'"). ' FROM table';
lengthThis is not a function, but a property. Some databases have “length” and others “len” as the function to measure the length of a string. To use this property:
$sql = "Select ".$db->length."(field) from table";
$rs = $db->Execute($sql);
randomThis is not a function, but a property. This is a string that holds the sql to generate a random number between 0.0 and 1.0 inclusive. substrThis is not a function, but a property. Some databases have “substr” and others “substring” as the function to retrieve a sub-string. To use this property:
$sql = "Select ".$db->substr."(field, $offset, $length) from table";
$rs = $db->Execute($sql);
For all databases, the 1st parameter of substr is the field, the 2nd is the offset (1-based) to the beginning of the sub-string, and the 3rd is the length of the sub-string.