/**
  * Execute a MySQL query
  *
  * @param $query Query to execute
  *
  * @return Query result handler
  **/
 function query($query)
 {
     global $CFG_GLPI, $DEBUG_SQL, $SQL_TOTAL_REQUEST;
     if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) {
         $SQL_TOTAL_REQUEST++;
         $DEBUG_SQL["queries"][$SQL_TOTAL_REQUEST] = $query;
         $TIMER = new Timer();
         $TIMER->start();
     }
     $res = @$this->dbh->query($query);
     if (!$res) {
         // no translation for error logs
         $error = "  *** MySQL query error:\n  SQL: " . addslashes($query) . "\n  Error: " . $this->dbh->error . "\n";
         $error .= toolbox::backtrace(false, 'DBmysql->query()', array('Toolbox::backtrace()'));
         Toolbox::logInFile("sql-errors", $error);
         if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) {
             $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST] = $this->error();
         }
     }
     if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) {
         $TIME = $TIMER->getTime();
         $DEBUG_SQL["times"][$SQL_TOTAL_REQUEST] = $TIME;
     }
     return $res;
 }
Exemple #2
0
 /**
  * Prepare a MySQL query
  *
  * @param $query Query to prepare
  *
  * @return a statement object or FALSE if an error occurred.
  **/
 function prepare($query)
 {
     $res = @$this->dbh->prepare($query);
     if (!$res) {
         // no translation for error logs
         $error = "  *** MySQL prepare error:\n  SQL: " . addslashes($query) . "\n  Error: " . $this->dbh->error . "\n";
         $error .= toolbox::backtrace(false, 'DBmysql->prepare()', array('Toolbox::backtrace()'));
         Toolbox::logInFile("sql-errors", $error);
         if (class_exists('GlpitestSQLError')) {
             // For unit test
             throw new GlpitestSQLError($error);
         }
         if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) {
             $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST] = $this->error();
         }
     }
     return $res;
 }