示例#1
0
文件: Db.php 项目: miaokuan/wee
 /**
  * Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a MySQLi_Result object. For other successful queries mysqli_query() will return TRUE.
  */
 public function query($sql)
 {
     $this->lastSql = $sql;
     $begin = intval(microtime(true) * 1000000);
     $this->connect();
     $res = $this->mysqli->query($sql);
     #reconnect max times 3
     for ($i = 0; $i < 3; $i++) {
         if (in_array($this->mysqli->errno, $this->retryErrno)) {
             Log::warning("db reconnect. [errno:" . $this->mysqli->errno . " error:" . $this->mysqli->error . " sql:{$sql}]");
             usleep(100000);
             $this->connect(true);
             $res = $this->mysqli->query($sql);
         } else {
             break;
         }
     }
     if (false === $res) {
         Log::warning("db error. [errno:" . $this->mysqli->errno . " error:" . $this->mysqli->error . " sql: {$sql}]");
     }
     $this->lastCost = intval(microtime(true) * 1000000) - $begin;
     $this->totalCost += $this->lastCost;
     if ($this->profile) {
         Log::debug('query success. [cost: ' . $this->lastCost . 'us] [sql: ' . $sql . ']');
     }
     return $res;
 }