bi..."/>

zencart中的数据库操作

2,017次阅读
没有评论

1、最简单的一个

$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;
}

其中TABLE_PRODUCTS需要在/includes/filenames.php中定义。
官方有句话:
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.
2、绑定参数

$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;
}

3、循环读数据操作

global $db;
$sql = "select title, code, value, last_updated from " . TABLE_CURRENCIES;
$result = $db->Execute($sql);

if ($result->RecordCount() > 0) {
  while (!$result->EOF) {
    echo '

Currency name: ' . $result->fields['title']; echo ', code: ' . $result->fields['code']; echo ', Exchange Rate: ' . $result->fields['value']; echo '

'; $result->MoveNext(); } } else { echo '

Sorry, no currencies found.

'; }

4、查询insert_id

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;

5、插入与更新函数
zen_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link')

正文完
 

公众号