v5:userguide:oracle_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
userguide:oracle_tutorial [2015/12/26 18:35] – mnewnham | userguide:oracle_tutorial [2015/12/27 15:20] – mnewnham | ||
---|---|---|---|
Line 127: | Line 127: | ||
And if you are interested in having the data returned in a 2-dimensional array, you can use: | And if you are interested in having the data returned in a 2-dimensional array, you can use: | ||
- | $arr = $db-> | + | |
Now to obtain only the first row as an array: | Now to obtain only the first row as an array: | ||
- | $arr = $db-> | + | |
Or to retrieve only the first field of the first row: | Or to retrieve only the first field of the first row: | ||
- | $arr = $db-> | + | |
For easy pagination support, we provide the SelectLimit function. The following will perform a select query, limiting it to 100 rows, starting from row 201 (row 1 being the 1st row): | For easy pagination support, we provide the SelectLimit function. The following will perform a select query, limiting it to 100 rows, starting from row 201 (row 1 being the 1st row): | ||
- | $offset = 200; $limitrows = 100; | + | |
- | $rs = $db-> | + | $rs = $db-> |
The $offset parameter is optional. | The $offset parameter is optional. | ||
Line 144: | Line 145: | ||
Using Prepare() For Frequently Used Statements | Using Prepare() For Frequently Used Statements | ||
- | Prepare( ) is for compiling frequently used SQL statement for reuse. For example, suppose we have a large array which needs to be inserted into an Oracle database. The following will result in a massive speedup in query execution (at least 20-40%), as the SQL statement only needs to be compiled once: | + | Prepare() is for compiling frequently used SQL statement for reuse. For example, suppose we have a large array which needs to be inserted into an Oracle database. The following will result in a massive speedup in query execution (at least 20-40%), as the SQL statement only needs to be compiled once: |
<code php> | <code php> | ||
Line 179: | Line 180: | ||
Note that LogError( ) is a user-defined function, and not part of ADOdb. | Note that LogError( ) is a user-defined function, and not part of ADOdb. | ||
- | Inserting LOBs is more complicated. | + | Inserting LOBs is more complicated. |
<code php> | <code php> | ||
Line 233: | Line 234: | ||
</ | </ | ||
- | PrepareSP( ) is a special function that knows about bind parameters. The main limitation currently is that IN OUT parameters do not work. | + | [[reference: |
Bind Parameters and REF CURSORs | Bind Parameters and REF CURSORs | ||
- | We could also rewrite the REF CURSOR example to use InParameter | + | We could also rewrite the REF CURSOR example to use InParameter: |
+ | <code php> | ||
$stmt = $db-> | $stmt = $db-> | ||
$input = ' | $input = ' | ||
Line 244: | Line 246: | ||
$rs = $db-> | $rs = $db-> | ||
while ($arr = $rs-> | while ($arr = $rs-> | ||
- | Bind Parameters and LOBs | + | </ |
+ | |||
+ | ===== Bind Parameters and LOBs ===== | ||
You can also operate on LOBs. In this example, we have IN and OUT parameters using CLOBs. | You can also operate on LOBs. In this example, we have IN and OUT parameters using CLOBs. | ||
- | + | <code php> | |
- | $text = 'test test test'; | + | $text = 'test test test'; |
- | $sql = " | + | $sql = " |
- | $stmt = $conn -> PrepareSP($sql); | + | $stmt = $conn -> PrepareSP($sql); |
- | $conn -> InParameter($stmt, | + | $conn -> InParameter($stmt, |
- | $rs = ''; | + | $rs = ''; |
- | $conn -> OutParameter($stmt, | + | $conn -> OutParameter($stmt, |
- | $conn -> Execute($stmt); | + | $conn -> Execute($stmt); |
- | echo " | + | echo " |
+ | </ | ||
Similarly, you can use the constant OCI_B_BLOB to indicate that you are using BLOBs. | Similarly, you can use the constant OCI_B_BLOB to indicate that you are using BLOBs. | ||
- | Reusing Bind Parameters with CURSOR_SHARING=FORCE | + | ====Reusing Bind Parameters with CURSOR_SHARING=FORCE |
Many web programmers do not care to use bind parameters, and prefer to enter the SQL directly. So instead of: | Many web programmers do not care to use bind parameters, and prefer to enter the SQL directly. So instead of: | ||
- | $arr = $db-> | + | |
They prefer entering the values inside the SQL: | They prefer entering the values inside the SQL: | ||
- | $arr = $db-> | + | |
This reduces Oracle performance because Oracle will reuse compiled SQL which is identical to previously compiled SQL. The above example with the values inside the SQL is unlikely to be reused. As an optimization, | This reduces Oracle performance because Oracle will reuse compiled SQL which is identical to previously compiled SQL. The above example with the values inside the SQL is unlikely to be reused. As an optimization, | ||
- | ALTER SESSION SET CURSOR_SHARING=FORCE | + | |
This will force Oracle to convert all such variables (eg. the 7900 value) into constant bind parameters, improving SQL reuse. | This will force Oracle to convert all such variables (eg. the 7900 value) into constant bind parameters, improving SQL reuse. | ||
- | More speedup tips. | + | ===== More speedup tips ===== |
- | + | ||
- | + | ||
- | 7. Dates and Datetime in ADOdb | + | ==== Dates and Datetime in ADOdb ==== |
There are two things you need to know about dates in ADOdb. | There are two things you need to know about dates in ADOdb. | ||
Line 283: | Line 289: | ||
Secondly, since Oracle treats dates and datetime as the same data type, we decided not to display the time in the default date format. So on login, ADOdb will set the NLS_DATE_FORMAT to ' | Secondly, since Oracle treats dates and datetime as the same data type, we decided not to display the time in the default date format. So on login, ADOdb will set the NLS_DATE_FORMAT to ' | ||
+ | <code php> | ||
$db = NewADOConnection(' | $db = NewADOConnection(' | ||
$db-> | $db-> | ||
$db-> | $db-> | ||
+ | </ | ||
+ | |||
Or execute: | Or execute: | ||
- | $sql = quot;ALTER SESSION SET NLS_DATE_FORMAT = ' | + | <code php> |
+ | $sql = "ALTER SESSION SET NLS_DATE_FORMAT = ' | ||
$db-> | $db-> | ||
+ | </ | ||
+ | |||
If you are not concerned about date portability and do not use ADOdb' | If you are not concerned about date portability and do not use ADOdb' | ||
- | 8. Database Portability Layer | + | ===== Database Portability Layer ===== |
ADOdb provides the following functions for portably generating SQL functions as strings to be merged into your SQL statements: | ADOdb provides the following functions for portably generating SQL functions as strings to be merged into your SQL statements: | ||
- | Function Description | + | ^Function^Description^ |
- | DBDate($date) Pass in a UNIX timestamp or ISO date and it will convert it to a date string formatted for INSERT/ | + | |DBDate($date)|Pass in a UNIX timestamp or ISO date and it will convert it to a date string formatted for INSERT/ |
- | DBTimeStamp($date) Pass in a UNIX timestamp or ISO date and it will convert it to a timestamp string formatted for INSERT/ | + | |DBTimeStamp($date)|Pass in a UNIX timestamp or ISO date and it will convert it to a timestamp string formatted for INSERT/ |
- | SQLDate($date, | + | |SQLDate($date, |
- | OffsetDate($date, | + | |OffsetDate($date, |
- | Concat($s1, $s2, ...) Portably concatenate strings. Alternatively, | + | |Concat($s1, $s2, ...)|Portably concatenate strings. Alternatively, |
- | IfNull($fld, | + | |IfNull($fld, |
- | Param($name) Generates bind placeholders, | + | |Param($name)|Generates bind placeholders, |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
- | $db-> | + | |$db-> |
ADOdb also provides multiple oracle oci8 drivers for different scenarios: | ADOdb also provides multiple oracle oci8 drivers for different scenarios: | ||
Line 336: | Line 349: | ||
The author' | The author' | ||
- | ===== Connection Examples ===== | ||
- | |||
- | Just in case you are having problems connecting to Oracle, here are some examples: | ||
- | |||
- | a. PHP and Oracle reside on the same machine, use default SID, with non-persistent connections: | ||
- | |||
- | $conn = NewADOConnection(' | ||
- | $conn-> | ||
- | b. TNS Name defined in tnsnames.ora (or ONAMES or HOSTNAMES), eg. ' | ||
- | |||
- | $conn = NewADOConnection(' | ||
- | $conn-> | ||
- | |||
- | or | ||
- | |||
- | $conn-> | ||
- | |||
- | c. Host Address and SID | ||
- | |||
- | $conn-> | ||
- | $conn-> | ||
- | |||
- | d. Host Address and Service Name | ||
- | |||
- | $conn-> | ||
- | |||
- | e. Oracle connection string: | ||
- | |||
- | $cstr = " | ||
- | $conn-> | ||
- | |||
- | f. ADOdb data source names (dsn): | ||
- | |||
- | $dsn = ' | ||
- | $conn = ADONewConnection($dsn); | ||
- | |||
- | $dsn = ' | ||
- | $conn = ADONewConnection($dsn); | ||
- | |||
- | $dsn = ' | ||
- | $conn = ADONewConnection($dsn); | ||
- | |||
- | With ADOdb data source names, you don't have to call Connect( ) or PConnect( ). | ||
v5/userguide/oracle_tutorial.txt · Last modified: 2019/12/29 21:36 by mnewnham