v5:reference:connection:param
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
v5:reference:connection:param [2017/05/17 19:18] – mnewnham | v5:reference:connection:param [2021/02/26 01:35] (current) – Call with falsy values: param(false) vs param(0) - see #682 dregad | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
<WRAP right box> | <WRAP right box> | ||
+ | ==See Also== | ||
+ | [[v5: | ||
== Syntax == | == Syntax == | ||
string param( | string param( | ||
Line 11: | Line 13: | ||
This method is used with [[v5: | This method is used with [[v5: | ||
+ | |||
+ | On RDBMS with positional (numbered) query parameters such as PostgreSQL, calling '' | ||
+ | |||
+ | <WRAP tip> | ||
+ | Note that calling '' | ||
+ | |||
+ | Compare that with '' | ||
+ | |||
+ | This is illustrated in the examples below. | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
------------------------------ | ------------------------------ | ||
===== Usage ===== | ===== Usage ===== | ||
+ | |||
+ | This is an example of how to use param() to build SQL queries: | ||
+ | |||
<code php> | <code php> | ||
- | /* | + | $sql1 = ' |
- | * Connection to Oracle database | + | . 'name = ' |
- | */ | + | . 'total = ' . $db-> |
- | print $db-> | + | |
- | /* | + | // Reset param count as a standalone operation then build the query |
- | * prints | + | $db-> |
- | */ | + | $sql2 = 'SELECT * FROM accounts WHERE id = ' . $db-> |
+ | |||
+ | // Reset param count with a " | ||
+ | $sql3 = ' | ||
+ | . 'name = ' . $db-> | ||
+ | . ' | ||
</ | </ | ||
- | <code php> | + | |
- | /* | + | |
- | * Connection to IBM DB2 database | + | |
- | */ | + | See below for the code's output with various database drivers. |
- | print $db->param(' | + | |
- | /* | + | ==== MySQL, |
- | * prints | + | <code> |
- | */ | + | $sql1: SELECT |
+ | $sql2: SELECT | ||
+ | $sql3: SELECT | ||
</ | </ | ||
+ | |||
+ | ==== Oracle (oci8) ==== | ||
+ | < | ||
+ | $sql1: SELECT * FROM accounts WHERE name = :account AND total = :amount | ||
+ | $sql2: SELECT * FROM accounts WHERE id = :id | ||
+ | $sql3: SELECT * FROM accounts WHERE name = :0 AND status = :1 | ||
+ | </ | ||
+ | |||
+ | ==== PostgreSQL ==== | ||
+ | < | ||
+ | $sql1: SELECT * FROM accounts WHERE name = $1 AND total = $2 | ||
+ | $sql2: SELECT * FROM accounts WHERE id = $1 | ||
+ | $sql3: SELECT * FROM accounts WHERE name = $1 AND status = $2 | ||
+ | </ | ||
+ | |||
+ | Without the parameter reset, //$sql2// would be '' | ||
v5/reference/connection/param.1495041481.txt.gz · Last modified: 2017/05/17 19:18 by mnewnham