示例#1
0
文件: DB.php 项目: voku/simple-mysqli
 /**
  * Execute a sql-multi-query.
  *
  * @param string $sql
  *
  * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br />
  *                        "boolean" by only "<b>INSERT</b>"-queries<br />
  *                        "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br />
  *                        "boolean" by only by e.g. "DROP"-queries<br />
  *
  * @throws \Exception
  */
 public function multi_query($sql)
 {
     if (!$this->isReady()) {
         return false;
     }
     if (!$sql || $sql === '') {
         $this->_debug->displayError('Can\'t execute an empty Query', false);
         return false;
     }
     $query_start_time = microtime(true);
     $resultTmp = \mysqli_multi_query($this->link, $sql);
     $query_duration = microtime(true) - $query_start_time;
     $this->_debug->logQuery($sql, $query_duration, 0);
     $returnTheResult = false;
     $result = array();
     if ($resultTmp) {
         do {
             $resultTmpInner = \mysqli_store_result($this->link);
             if ($resultTmpInner instanceof \mysqli_result) {
                 $returnTheResult = true;
                 $result[] = new Result($sql, $resultTmpInner);
             } else {
                 $errorMsg = \mysqli_error($this->link);
                 // is the query successful
                 if ($resultTmpInner === true || !$errorMsg) {
                     $result[] = true;
                 } else {
                     $result[] = $this->queryErrorHandling($errorMsg, $sql);
                 }
             }
         } while (\mysqli_more_results($this->link) === true ? \mysqli_next_result($this->link) : false);
     } else {
         $errorMsg = \mysqli_error($this->link);
         if ($this->_debug->checkForDev() === true) {
             echo "Info: maybe you have to increase your 'max_allowed_packet = 30M' in the config: 'my.conf' \n<br />";
             echo 'Error:' . $errorMsg;
         }
         $this->_debug->mailToAdmin('SQL-Error in mysqli_multi_query', $errorMsg . ":\n<br />" . $sql);
     }
     // return the result only if there was a "SELECT"-query
     if ($returnTheResult === true) {
         return $result;
     }
     if (!in_array(false, $result, true)) {
         return true;
     } else {
         return false;
     }
 }
示例#2
0
 /**
  * Error-handling for the sql-query.
  *
  * @param string $errorMsg
  * @param string $sql
  *
  * @throws \Exception
  *
  * @return bool
  */
 private function queryErrorHandling($errorMsg, $sql)
 {
     if ($errorMsg === 'DB server has gone away' || $errorMsg === 'MySQL server has gone away') {
         static $reconnectCounter;
         // exit if we have more then 3 "DB server has gone away"-errors
         if ($reconnectCounter > 3) {
             $this->_debug->mailToAdmin('SQL-Fatal-Error', $errorMsg . ":\n<br />" . $sql, 5);
             throw new \Exception($errorMsg);
         } else {
             $this->_debug->mailToAdmin('SQL-Error', $errorMsg . ":\n<br />" . $sql);
             // reconnect
             $reconnectCounter++;
             $this->_db->reconnect(true);
             // re-run the current query
             return $this->execute();
         }
     } else {
         $this->_debug->mailToAdmin('SQL-Warning', $errorMsg . ":\n<br />" . $sql);
         // this query returned an error, we must display it (only for dev) !!!
         $this->_debug->displayError($errorMsg . ' | ' . $sql);
     }
     return false;
 }