destoon 数据库操作

1、插入数据

 $db->query("INSERT INTO ".get_table($mid)." ($sqlk) VALUES ($sqlv)");
 $itemid = $db->insert_id();

2、获取一条数据

$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $condition order by statid desc");
  if($r['num']>0){
   exit('{"status":"0","msg":"重复"}'); 
  }else{
  
  }

函数在include/db_mysql.class.php 中定义

function query($sql, $type = '', $ttl = 0, $save_id = false) {
  #echo $sql;echo '<br/>';
  $select = strtoupper(substr($sql, 0, 7)) == 'SELECT ' ? 1 : 0;
  if($this->ttl > 0 && $type == 'CACHE' && $select) {
   $this->cursor = 0;
   $this->cache_id = md5($sql);
   if($this->cids) $this->cache_ids[] = $this->cache_id;
   $this->result = array();
   $this->cache_ttl = ($ttl ? $ttl : $this->ttl) + mt_rand(-10, 30);
   return $this->_query($sql);
  }
  if(!$save_id) $this->cache_id = 0;
  $func = $type == 'UNBUFFERED' ? 'mysql_unbuffered_query' : 'mysql_query';
  if(!($query = $func($sql, $this->connid))) $this->halt('MySQL Query Error', $sql);
  $this->querynum++;
  return $query;
 }

function get_one($sql, $type = '', $ttl = 0) {
  $sql = str_replace(array('select ', ' limit '), array('SELECT ', ' LIMIT '), $sql);
  if(strpos($sql, 'SELECT ') !== false && strpos($sql, ' LIMIT ') === false) $sql .= ' LIMIT 0,1';
  $query = $this->query($sql, $type, $ttl);
  $r = $this->fetch_array($query);
  $this->free_result($query);
  return $r;
 }

function count($table, $condition = '', $ttl = 0) {
  global $DT_TIME;
  $sql = 'SELECT COUNT(*) as amount FROM '.$table;
  if($condition) $sql .= ' WHERE '.$condition;
  $r = $this->get_one($sql, $ttl ? 'CACHE' : '', $ttl);
  return $r ? $r['amount'] : 0;
 }

 function fetch_array($query, $result_type = MYSQL_ASSOC) {
  return $this->cache_id ? $this->_fetch_array($query) : mysql_fetch_array($query, $result_type);
 }

 function affected_rows() {
  return mysql_affected_rows($this->connid);
 }

 function num_rows($query) {
  return mysql_num_rows($query);
 }

 function num_fields($query) {
  return mysql_num_fields($query);
 }

 function result($query, $row) {
  return @mysql_result($query, $row);
 }

 function free_result($query) {
  if(is_resource($query) && get_resource_type($query) === 'mysql result') {
   return @mysql_free_result($query);
  }
 }

 function insert_id() {
  return mysql_insert_id($this->connid);
 }

 function fetch_row($query) {
  return mysql_fetch_row($query);
 }

原文地址:https://www.cnblogs.com/djiz/p/6414236.html