- $result = @$this->db->query($sql);
- break;
- case 'mysql':
- $result = @mysql_query($sql,$this->db);
- break;
- }
- $stamp2 = microtime(true);
- $duration = (float)($stamp2 - $stamp1);
-
- $a->save_timestamp($stamp1, "database");
-
- if (strtolower(substr($orig_sql, 0, 6)) != "select") {
- $a->save_timestamp($stamp1, "database_write");
- }
- if (x($a->config,'system') && x($a->config['system'],'db_log')) {
- if (($duration > $a->config["system"]["db_loglimit"])) {
- $duration = round($duration, 3);
- $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
- @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
- basename($backtrace[1]["file"])."\t".
- $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
- substr($sql, 0, 2000)."\n", FILE_APPEND);
- }
- }
-
- switch ($this->driver) {
- case 'pdo':
- $errorInfo = $this->db->errorInfo();
- if ($errorInfo) {
- $this->error = $errorInfo[2];
- $this->errorno = $errorInfo[1];
- }
- break;
- case 'mysqli':
- if ($this->db->errno) {
- $this->error = $this->db->error;
- $this->errorno = $this->db->errno;
- }
- break;
- case 'mysql':
- if (mysql_errno($this->db)) {
- $this->error = mysql_error($this->db);
- $this->errorno = mysql_errno($this->db);
- }
- break;
- }
- if (strlen($this->error)) {
- logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
- }
-
- if ($this->debug) {
-
- $mesg = '';
-
- if ($result === false) {
- $mesg = 'false';
- } elseif ($result === true) {
- $mesg = 'true';
- } else {
- switch ($this->driver) {
- case 'pdo':
- $mesg = $result->rowCount().' results'.EOL;
- break;
- case 'mysqli':
- $mesg = $result->num_rows.' results'.EOL;
- break;
- case 'mysql':
- $mesg = mysql_num_rows($result).' results'.EOL;
- break;
- }
- }
-
- $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
- . (($this->error) ? ' error: ' . $this->error : '')
- . EOL;
-
- logger('dba: ' . $str );
- }
-
- /**
- * If dbfail.out exists, we will write any failed calls directly to it,
- * regardless of any logging that may or may nor be in effect.
- * These usually indicate SQL syntax errors that need to be resolved.
- */
-
- if ($result === false) {
- logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
- if (file_exists('dbfail.out')) {
- file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
- }