![]() |
|
|||||||
|
|
Thread Tools | Display Modes |
|
#1
IP: 104.151.157.182
|
|||
|
|||
|
Basic Example
A sample database inquiry to retrieve the model number of a specified product number would occur like this: Code:
$theProductId = 25;
global $db;
$sql = "select products_model from " . TABLE_PRODUCTS . " where products_id = :productID:";
$sql = $db->bindVars($sql, ':productID:', $theProductId, 'integer');
$result = $db->Execute($sql);
if ($result->RecordCount() > 0) {
echo 'Model number = ' . $result->fields['products_model'];
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
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 roductID: is a placeholder which is handled by the next line: Code:
$sql = $db->bindVars($sql, ':productID:', $theProductId, 'integer'); roductID: with the value of $theProductId AFTER first ensuring that the $theProductId is an 'integer' value. (This is for security reasons so nobody can do an SQL injection hack against your script.) Additional bindVars datatypes besides 'integer' are listed below. Code:
$result = $db->Execute($sql); Code:
if ($result->RecordCount() > 0) {
<pre> echo 'Model number = ' . $result->fields['products_model'];
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
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;
$sql = "select title, code, value, last_updated from " . TABLE_CURRENCIES;
$result = $db->Execute($sql);
if ($result->RecordCount() > 0) {
while (!$result->EOF) {
echo '<p>Currency name: ' . $result->fields['title'];
echo ', code: ' . $result->fields['code'];
echo ', Exchange Rate: ' . $result->fields['value'];
echo '</p>';
$result->MoveNext();
}
} else {
echo '<p>Sorry, no currencies found.</p>';
}
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'];
echo ', code: ' . $result->fields['code'];
echo ', Exchange Rate: ' . $result->fields['value'];
echo '</p>';
Code:
$result->MoveNext(); } Code:
} else {
echo '<p>Sorry, no currencies found.</p>';
}
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; $sql = "insert into " . TABLE_SOMETHING . " (fieldname1, fieldname2) values (:value1:, :value2:)"; $sql = $db->bindVars($sql, ':value1:', $valueOne, 'integer'); $sql = $db->bindVars($sql, ':value2:', $valueTwo, 'string'); $result = $db->Execute($sql); $newRecordId = $db->Insert_ID(); echo 'The new record added was number: ' . $newRecordId; 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. Last edited by Abraham : 2016-05-15 at 07:01 AM |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| zen cart – clear all customer data from zen cart website database | Uinta | ZenCart | 0 | 2015-12-27 01:17 PM |
| Simple Steps to Change Your Table Prefix in WordPress | Abby | WordPress | 0 | 2014-04-14 03:40 PM |
| How to Install an Zen Cart Template | topvip | ZenCart | 0 | 2009-08-23 05:35 PM |
| X-Cart Multiple Input Validation Holes Permit SQL Injection and Cross-Site Scripting | topvip | X-Cart | 0 | 2009-07-21 10:03 AM |
| x-cart Template Editing Guide for 4.1.x | topvip | X-Cart | 0 | 2009-04-15 11:42 PM |