![]() |
Zen Cart Database Abstraction Layer
Basic Example
A sample database inquiry to retrieve the model number of a specified product number would occur like this: Code:
$theProductId = 25;Code:
global $db;Code:
$sql = "select products_model from " . TABLE_PRODUCTS . " where products_id = :productID:";The TABLE_PRODUCTS constant is used in order to support table-prefixes, since the constant will automatically contain the prefix, according to the logic in the /includes/filenames.php script and the DB_PREFIX value in your /includes/configure.php file. Note the :productID: is a placeholder which is handled by the next line: Code:
$sql = $db->bindVars($sql, ':productID:', $theProductId, 'integer');Code:
$result = $db->Execute($sql);Code:
if ($result->RecordCount() > 0) {Example of looping through multiple records A simple example to list all the currencies in your database, along with the currently-configured exchange rates: Code:
global $db;Code:
global $db;Code:
$sql = "select title, code, value, last_updated from " . TABLE_CURRENCIES;NOTE: There is no use of bindVars() here, because there is no parameter supplied as selection criteria for the query. Code:
$result = $db->Execute($sql);Code:
if ($result->RecordCount() > 0) {Code:
while (!$result->EOF) {Code:
echo '<p>Currency name: ' . $result->fields['title'];Code:
$result->MoveNext();Code:
} else {Finding out what record number was inserted When you run a query with an INSERT statement in it, you can find out the record number assigned to the new line you added by referencing $db->Insert_ID(), like this: Code:
global $db;Available datatypes for use in bindVars() parameters include the following. In practice, the first 4 are used the most: integer - ensures that the supplied data is an integer string - ensures that the supplied data is a string, and escapes any quotes, and adds quotes around it noquotestring - ensures that the supplied data is a string, and escapes any quotes in it, but doesn't add quotes around it float - ensures that the supplied data is numeric, including ensuring that a blank string is converted to a 0 currency - similar to 'string' date - similar to 'string' enum - similar to mysql enum type: forces data to be within available list of choices regexp - checks that the supplied parameter is formatted as a regular expression Escaping Content Many people are tempted to directly use "mysql_escape_string()" function calls, or merely use "add_slashes()". But the BEST way to do it is with BindVars as described above. However, if you have a strong objection to using the safeties and protections that BindVars offers, and you know why you're not using it, then you could use $db->prepare_input($value). For optimal future-proofing of your code, ALWAYS USE ZEN CART FUNCTIONS FOR ACCESSING THE DATABASE, and don't make direct calls to mysql_xxxxx() or mysqli_xxxxx() functions. |
| All times are GMT +8. The time now is 10:43 AM. |
Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.