예제 #1
0
 /**
  * Retrieve one variable from the database.
  *
  * Executes a SQL query and returns the value from the SQL result.
  * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified.
  * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
  *
  * @since 0.71
  *
  * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
  * @param int         $x     Optional. Column of value to return. Indexed from 0.
  * @param int         $y     Optional. Row of value to return. Indexed from 0.
  * @return string|null Database query result (as string), or null on failure
  */
 public function get_var($query = null, $x = 0, $y = 0)
 {
     $this->func_call = "\$db->get_var(\"{$query}\", {$x}, {$y})";
     if ($this->check_current_query && $this->check_safe_collation($query)) {
         $this->check_current_query = false;
     }
     if ($query && $x == 0 && $y == 0) {
         $result = sqlsrv_query($this->dbh, $query);
         // If there is an error, first attempt to translate
         $errors = sqlsrv_errors();
         if (!empty($errors) && is_array($errors)) {
             switch ($errors[0]['code']) {
                 case 102:
                 case 145:
                 case 156:
                 case 195:
                 case 207:
                 case 241:
                 case 261:
                 case 321:
                 case 1018:
                 case 8120:
                 case 8127:
                     if (getenv('ProjectNamiLogTranslate')) {
                         $begintransmsg = date("Y-m-d H:i:s") . " -- Begin translation attempt: {$query} \n";
                         error_log($begintransmsg, 3, 'D:\\home\\LogFiles\\translate.log');
                     }
                     $sqltranslate = new SQL_Translations(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
                     $query = $sqltranslate->translate($query);
                     if (getenv('ProjectNamiLogTranslate')) {
                         $endtransmsg = date("Y-m-d H:i:s") . " -- Translation result: {$query} \n";
                         error_log($endtransmsg, 3, 'D:\\home\\LogFiles\\translate.log');
                     }
                     $result = sqlsrv_query($this->dbh, $query);
             }
         }
         if (false === $result) {
             return null;
         }
         $row = sqlsrv_fetch_array($result);
         return $row[0];
     }
     if ($query) {
         $this->query($query);
     }
     // Extract var out of cached results based x,y vals
     if (!empty($this->last_result[$y])) {
         if (is_object($this->last_result[$y])) {
             $values = array_values(get_object_vars($this->last_result[$y]));
         } else {
             $values = array_values($this->last_result[$y]);
         }
     }
     // If there is a value return it else return null
     return isset($values[$x]) && $values[$x] !== '' ? $values[$x] : null;
 }