developing
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
developing [2015/07/06 05:53] – created mnewnham | developing [2015/07/12 17:13] – mnewnham | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Creating Applications With ADOdb ====== | ====== Creating Applications With ADOdb ====== | ||
+ | [[reference: | ||
===== Example 1: Select Statement ===== | ===== Example 1: Select Statement ===== | ||
Line 8: | Line 9: | ||
NB: A useful function that is not used in this example is SelectLimit, | NB: A useful function that is not used in this example is SelectLimit, | ||
- | < | + | < |
include(' | include(' | ||
- | |||
$conn = & | $conn = & | ||
- | |||
$conn-> | $conn-> | ||
- | |||
$recordSet = & | $recordSet = & | ||
- | |||
if (!$recordSet) | if (!$recordSet) | ||
+ | print $conn-> | ||
+ | else while (!$recordSet-> | ||
+ | print $recordSet-> | ||
+ | $recordSet-> | ||
+ | } | ||
- | print $conn-> | + | /* |
- | + | * Recordsets and connections are automatically closed at the end of the script | |
- | else | + | */ |
- | + | ||
- | while (!$recordSet-> | + | |
- | + | ||
- | print $recordSet-> | + | |
- | + | ||
- | | + | |
- | + | ||
- | } | + | |
- | + | ||
$recordSet-> | $recordSet-> | ||
- | |||
$conn-> | $conn-> | ||
- | < | + | </code> |
The $recordSet returned stores the current row in the $recordSet-> | The $recordSet returned stores the current row in the $recordSet-> | ||
The $recordSet-> | The $recordSet-> | ||
- | $db-> | + | <code php> |
- | + | $db-> | |
- | $rs1 = $db-> | + | $rs1 = $db-> |
- | + | $db-> | |
- | $db-> | + | $rs2 = $db-> |
- | + | print_r($rs1-> | |
- | $rs2 = $db-> | + | print_r($rs2-> |
+ | </ | ||
- | | ||
- | | ||
To get the number of rows in the select statement, you can use $recordSet-> | To get the number of rows in the select statement, you can use $recordSet-> | ||
- | Example 2: Advanced Select with Field Objects | + | ===== Example 2: Advanced Select with Field Objects |
Select a table, display the first two columns. If the second column is a date or timestamp, reformat the date to US format. | Select a table, display the first two columns. If the second column is a date or timestamp, reformat the date to US format. | ||
- | <? | + | <code php> |
include(' | include(' | ||
Line 102: | Line 93: | ||
- | ?> | + | </code> |
In this example, we check the field type of the second column using FetchField(). This returns an object with at least 3 fields. | In this example, we check the field type of the second column using FetchField(). This returns an object with at least 3 fields. | ||
Line 121: | Line 113: | ||
If the metatype is of type date or timestamp, then we print it using the user defined date format with UserDate(), which converts the PHP SQL date string format to a user defined one. Another use for MetaType() is data validation before doing an SQL insert or update. | If the metatype is of type date or timestamp, then we print it using the user defined date format with UserDate(), which converts the PHP SQL date string format to a user defined one. Another use for MetaType() is data validation before doing an SQL insert or update. | ||
- | Example 3: Inserting | + | ===== Example 3: Inserting |
Insert a row to the Orders table containing dates and strings that need to be quoted before they can be accepted by the database, eg: the single-quote in the word John' | Insert a row to the Orders table containing dates and strings that need to be quoted before they can be accepted by the database, eg: the single-quote in the word John' | ||
- | <? | + | <code php> |
include(' | include(' | ||
- | |||
$conn = & | $conn = & | ||
- | |||
- | |||
- | |||
$conn-> | $conn-> | ||
- | |||
$shipto = $conn-> | $shipto = $conn-> | ||
- | |||
- | |||
$sql = " | $sql = " | ||
- | |||
$sql .= " | $sql .= " | ||
+ | if ($conn-> | ||
+ | print 'error inserting: ' | ||
- | if ($conn->Execute($sql) === false) { | + | </code> |
- | print 'error inserting: '.$conn-> | + | In this example, we see the advanced date and quote handling facilities of ADOdb. The unix timestamp |
- | } | + | Observe the error-handling of the Execute statement. False is returned by Execute() if an error occurred. The error message for the last error that occurred is displayed in ErrorMsg(). Note: php_track_errors might have to be enabled for error messages to be saved. |
- | ?> | + | ===== Example 4: Debugging ===== |
- | In this example, we see the advanced date and quote handling facilities of ADOdb. The unix timestamp (which is a long integer) is appropriately formated for Access with DBDate(), and the right escape character is used for quoting the John's Old Shoppe, which is John'' | + | |
- | Observe the error-handling of the Execute statement. False is returned by Execute() if an error occured. The error message for the last error that occurred is displayed in ErrorMsg(). Note: php_track_errors might have to be enabled for error messages to be saved. | + | <code php> |
- | + | ||
- | Example 4: Debugging | + | |
- | + | ||
- | <? | + | |
include(' | include(' | ||
- | |||
$conn = & | $conn = & | ||
- | |||
$conn-> | $conn-> | ||
- | |||
$shipto = $conn-> | $shipto = $conn-> | ||
$sql = " | $sql = " | ||
- | |||
$sql .= " | $sql .= " | ||
$conn-> | $conn-> | ||
if ($conn-> | if ($conn-> | ||
- | ?> | + | </code> |
In the above example, we have turned on debugging by setting debug = true. This will display the SQL statement before execution, and also show any error messages. There is no need to call ErrorMsg() in this case. For displaying the recordset, see the rs2html() example. | In the above example, we have turned on debugging by setting debug = true. This will display the SQL statement before execution, and also show any error messages. There is no need to call ErrorMsg() in this case. For displaying the recordset, see the rs2html() example. | ||
Also see the section on Custom Error Handlers. | Also see the section on Custom Error Handlers. | ||
- | Example 5: MySQL and Menus | + | ===== Example 5: MySQL and Menus ===== |
Connect to MySQL database agora, and generate a < | Connect to MySQL database agora, and generate a < | ||
- | <? | + | <code php> |
include(' | include(' | ||
Line 197: | Line 174: | ||
print $rs-> | print $rs-> | ||
- | ?> | + | </code> |
Here we define a menu named GetCust, with the menu option 'Mary Rosli' selected. See GetMenu(). We also have functions that return the recordset as an array: GetArray(), and as an associative array with the key being the first column: GetAssoc(). | Here we define a menu named GetCust, with the menu option 'Mary Rosli' selected. See GetMenu(). We also have functions that return the recordset as an array: GetArray(), and as an associative array with the key being the first column: GetAssoc(). | ||
- | Example 6: Connecting to 2 Databases At Once | + | ===== Example 6: Connecting to 2 Databases At Once ===== |
- | <? | + | <code php> |
include(' | include(' | ||
Line 222: | Line 200: | ||
$conn2-> | $conn2-> | ||
- | ?> | + | </code> |
- | Example 7: Generating Update and Insert SQL | + | |
+ | ===== Example 7: Generating Update and Insert SQL ===== | ||
Since ADOdb 4.56, we support AutoExecute(), | Since ADOdb 4.56, we support AutoExecute(), | ||
- | + | <code php> | |
- | $record[" | + | $record[" |
- | $record[" | + | $record[" |
- | $record[" | + | $record[" |
- | $insertSQL = $conn-> | + | $insertSQL = $conn-> |
+ | </ | ||
and an UPDATE with: | and an UPDATE with: | ||
- | + | ||
- | $record[" | + | <code php> |
- | $record[" | + | $record[" |
- | $insertSQL = $conn-> | + | $record[" |
+ | $insertSQL = $conn-> | ||
+ | </ | ||
The rest of this section is out-of-date: | The rest of this section is out-of-date: | ||
Line 245: | Line 229: | ||
Before these functions can be called, you need to initialize the recordset by performing a select on the table. Idea and code by Jonathan Younger jyounger# | Before these functions can be called, you need to initialize the recordset by performing a select on the table. Idea and code by Jonathan Younger jyounger# | ||
- | <? | + | <code php> |
# | # | ||
Line 357: | Line 341: | ||
$conn-> | $conn-> | ||
- | ?> | + | </code> |
$ADODB_FORCE_TYPE | $ADODB_FORCE_TYPE | ||
The behaviour of AutoExecute(), | The behaviour of AutoExecute(), | ||
Line 372: | Line 357: | ||
+ | <code php> | ||
define(' | define(' | ||
Line 380: | Line 365: | ||
define(' | define(' | ||
+ | |||
+ | </ | ||
+ | |||
Thanks to Niko (nuko# | Thanks to Niko (nuko# | ||
Line 386: | Line 374: | ||
Since 4.62, the table name to be used can be overridden by setting $rs-> | Since 4.62, the table name to be used can be overridden by setting $rs-> | ||
- | Example 8: Implementing Scrolling with Next and Previous | + | ====== |
The following code creates a very simple recordset pager, where you can scroll from page to page of a recordset. | The following code creates a very simple recordset pager, where you can scroll from page to page of a recordset. | ||
+ | <code php> | ||
include_once(' | include_once(' | ||
Line 413: | Line 402: | ||
$pager-> | $pager-> | ||
+ | |||
+ | </ | ||
This will create a basic record pager that looks like this: | This will create a basic record pager that looks like this: | ||
- | |< << | + | |< << |
- | ID | + | |
- | First Name | + | ^ID^First Name^Last Name^Date Created^ |
- | Last Name | + | |36|Alan|Turing|Sat 06, Oct 2001| |
- | Date Created | + | |37|Serena|Williams|Sat 06, Oct 2001| |
- | 36 | + | |38|Yat Sun|Sun|Sat 06, Oct 2001| |
- | Alan | + | |39|Wai Hun|See|Sat 06, Oct 2001| |
- | Turing | + | |40|Steven|Oey|Sat 06, Oct 2001| |
- | Sat 06, Oct 2001 | + | |
- | 37 | + | |
- | Serena | + | |
- | Williams | + | |
- | Sat 06, Oct 2001 | + | |
- | 38 | + | |
- | Yat Sun | + | |
- | Sun | + | |
- | Sat 06, Oct 2001 | + | |
- | 39 | + | |
- | Wai Hun | + | |
- | See | + | |
- | Sat 06, Oct 2001 | + | |
- | 40 | + | |
- | Steven | + | |
- | Oey | + | |
- | Sat 06, Oct 2001 | + | |
Page 8/10 | Page 8/10 | ||
+ | |||
The number of rows to display at one time is controled by the Render($rows) method. If you do not pass any value to Render(), ADODB_Pager will default to 10 records per page. | The number of rows to display at one time is controled by the Render($rows) method. If you do not pass any value to Render(), ADODB_Pager will default to 10 records per page. | ||