v5:userguide:portable_sql
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
v5:userguide:portable_sql [2016/01/13 16:23] – ↷ Page moved from v5:reference:portable_sql to v5:userguide:portable_sql mnewnham | v5:userguide:portable_sql [2020/12/30 21:32] (current) – [Prepare/Execute] fix obvious variable case typos peterdd | ||
---|---|---|---|
Line 26: | Line 26: | ||
^Oracle|select * from (select * from table) where rownum <= 10| | ^Oracle|select * from (select * from table) where rownum <= 10| | ||
- | This feature of getting a subset of data is so useful that in the PHP class library ADOdb, we have a [[v5: | + | This feature of getting a subset of data is so useful that in the PHP class library ADOdb, we have a [[v5: |
<code php> | <code php> | ||
- | $connection-> | + | $connection-> |
</ | </ | ||
==== Fetch Modes Within Select Statements ==== | ==== Fetch Modes Within Select Statements ==== | ||
- | PHP allows you to retrieve database records as arrays. You can choose to have the arrays indexed by field name or number. However different low-level PHP database drivers are inconsistent in their indexing efforts. ADOdb allows you to determine your prefered mode. You set this by setting the variable [[v5: | + | PHP allows you to retrieve database records as arrays. You can choose to have the arrays indexed by field name or number. However different low-level PHP database drivers are inconsistent in their indexing efforts. ADOdb allows you to determine your prefered mode. You set this by setting the variable [[v5: |
* ADODB_FETCH_NUM (for numeric indexes) | * ADODB_FETCH_NUM (for numeric indexes) | ||
* ADODB_FETCH_ASSOC (using field names as an associative index). | * ADODB_FETCH_ASSOC (using field names as an associative index). | ||
Line 44: | Line 44: | ||
Another problem with SELECTs is that some databases do not return the number of rows retrieved from a select statement. This is because the highest performance databases will return records to you even before the last record has been found. | Another problem with SELECTs is that some databases do not return the number of rows retrieved from a select statement. This is because the highest performance databases will return records to you even before the last record has been found. | ||
- | In ADOdb, [[v5: | + | In ADOdb, [[v5: |
- | If you prefer to set '' | + | If you prefer to set '' |
<code php> | <code php> | ||
- | $rs = $db->Execute(" | + | $rs = $db->execute(" |
- | $numrows = $rs->PO_RecordCount(' | + | $numrows = $rs->po_recordCount(' |
</ | </ | ||
Line 60: | Line 60: | ||
<code php> | <code php> | ||
- | $connection-> | + | $connection-> |
- | $connection-> | + | $connection-> |
# some operation | # some operation | ||
- | if ($ok) $connection-> | + | if ($ok) $connection-> |
- | else $connection-> | + | else $connection-> |
</ | </ | ||
Line 98: | Line 98: | ||
===== Inserting Records ===== | ===== Inserting Records ===== | ||
- | When you create records, you need to generate unique id's for each record. There are two common techniques: | + | When you create records, you need to generate unique id's for each record. There are two common techniques: |
+ | * Auto-increment | ||
+ | * Sequences | ||
- | Auto-incrementing | + | Auto-increment |
+ | |||
+ | Different tables can have different sequences. | ||
<code php> | <code php> | ||
- | $id = $connection-> | + | $id = $connection-> |
- | $connection-> | + | $connection-> |
- | values ($id, $firstname, $lastname)" | + | values ($id, $firstname, $lastname)" |
</ | </ | ||
- | For databases that do not support | + | For databases that do not provide native |
===== Binding ===== | ===== Binding ===== | ||
Line 116: | Line 120: | ||
===== Prepare/ | ===== Prepare/ | ||
<code php> | <code php> | ||
- | $stmt = $db->Prepare(' | + | $stmt = $db->prepare(' |
- | $rs = $db->Execute($stmt, array($id,' | + | $rs = $db->execute($stmt, array($id,' |
/* | /* | ||
Oracle uses named bind placeholders, | Oracle uses named bind placeholders, | ||
*/ | */ | ||
- | $sql = ' | + | $sql = ' |
# generates ' | # generates ' | ||
# or ' | # or ' | ||
- | $stmt = $DB->Prepare($sql); | + | $stmt = $db->prepare($sql); |
- | $stmt = $DB->Execute($stmt, | + | $stmt = $db->execute($stmt, array(' |
</ | </ | ||
Line 170: | Line 174: | ||
AGE N(16) DEFAULT 0 | AGE N(16) DEFAULT 0 | ||
"; | "; | ||
- | $sql1 = $datadict-> | + | $sql1 = $datadict-> |
- | $sql2 = $datadict-> | + | $sql2 = $datadict-> |
</ | </ | ||
Line 182: | Line 186: | ||
<code php> | <code php> | ||
- | $date1 = $connection-> | + | $date1 = $connection-> |
- | $date2 = $connection-> | + | $date2 = $connection-> |
</ | </ | ||
We also provide functions to convert database dates to Unix timestamps: | We also provide functions to convert database dates to Unix timestamps: | ||
<code php> | <code php> | ||
- | $unixts = $recordset-> | + | $unixts = $recordset-> |
</ | </ | ||
- | For date calculations, | + | For date calculations, |
<code php> | <code php> | ||
- | $sql = ' | + | $sql = ' |
</ | </ | ||
- | The maximum length of a char/ | + | The maximum length of a char/ |
<code php> | <code php> | ||
# for oracle | # for oracle | ||
- | $conn->Execute(' | + | $conn->execute(' |
- | $conn->UpdateBlob(' | + | $conn->updateBlob(' |
# non-oracle databases | # non-oracle databases | ||
- | $conn->Execute(' | + | $conn->execute(' |
- | $conn->UpdateBlob(' | + | $conn->updateBlob(' |
</ | </ | ||
Line 213: | Line 217: | ||
In general, I avoid using nulls except for dates and default all my numeric and character fields to 0 or the empty string. This maintains consistency with PHP, where empty strings and zero are treated as equivalent, and avoids SQL ambiguities when you use the ANY and EXISTS operators. However if your database has significant amounts of missing or unknown data, using nulls might be a good idea. | In general, I avoid using nulls except for dates and default all my numeric and character fields to 0 or the empty string. This maintains consistency with PHP, where empty strings and zero are treated as equivalent, and avoids SQL ambiguities when you use the ANY and EXISTS operators. However if your database has significant amounts of missing or unknown data, using nulls might be a good idea. | ||
- | ADOdb also supports a portable [[v5: | + | ADOdb also supports a portable [[v5: |
===== Stored Procedures ===== | ===== Stored Procedures ===== | ||
Line 233: | Line 237: | ||
} | } | ||
# @RETVAL = SP_RUNSOMETHING @myid, | # @RETVAL = SP_RUNSOMETHING @myid, | ||
- | $stmt = $db->PrepareSP($sql); | + | $stmt = $db->prepareSP($sql); |
$db-> | $db-> | ||
- | $db->Parameter($stmt, | + | $db->parameter($stmt, |
# true indicates output parameter | # true indicates output parameter | ||
- | $db->Parameter($stmt, | + | $db->parameter($stmt, |
- | $db->Execute($stmt); | + | $db->execute($stmt); |
</ | </ | ||
Line 249: | Line 253: | ||
<code php> | <code php> | ||
$sqlGetPassword = ' | $sqlGetPassword = ' | ||
- | $sqlSearchKeyword = quot;SELECT * FROM articles WHERE match (title, | + | $sqlSearchKeyword = "SELECT * FROM articles WHERE match (title, |
</ | </ | ||
Line 259: | Line 263: | ||
include_once(" | include_once(" | ||
- | $db = NewADOConnection($database); | + | $db = newADOConnection($database); |
- | $db->PConnect(...) or die(' | + | $db->pConnect(...) or die(' |
# search for a keyword $word | # search for a keyword $word | ||
- | $rs = $db->Execute(sprintf($sqlSearchKeyWord, | + | $rs = $db->execute(sprintf($sqlSearchKeyWord, |
</ | </ | ||
- | Note that we quote the $word variable using the [[v5: | + | Note that we quote the $word variable using the [[v5: |
===== Final Thoughts ===== | ===== Final Thoughts ===== |
v5/userguide/portable_sql.1452698627.txt.gz · Last modified: 2017/04/21 11:39 (external edit)