首页 > 解决方案 > PHP 致命错误:未捕获的错误:在 null 上调用成员函数 query()

问题描述

因为有些日子我得到这个致命错误: PHP 致命错误:未捕获的错误:在 null in 上调用成员函数 query() 之前它没有问题。我没有改变什么。我该如何解决这个问题?我在服务器上有 php v7.2.24-0ubuntu0.18.04.4。

因为有些日子我得到这个致命错误: PHP 致命错误:未捕获的错误:在 null in 上调用成员函数 query() 之前它没有问题。我没有改变什么。我该如何解决这个问题?我在服务器上有 php v7.2.24-0ubuntu0.18.04.4。

class Database extends MySQLi {
  private $application = null;
  private $dbData = null;
  private $countQueries = 0;
  private $queryLog = array();
  private $lastQueryTime = 0;

  private static $connections = array();

  public function __construct($application) {
    $this->application = $application;

    $this->dbData = DBData::getData($this->application);

    $this->dbConnect();
  }

  public function __destruct() {
    unset(self::$connections[$this->application]);

    $this->close();
  }

  private function dbConnect() {
    parent::init();
    parent::options(MYSQLI_OPT_LOCAL_INFILE, true);
    $erg = @parent::real_connect($this->dbData['host'], $this->dbData['user'], $this->dbData['pwd'], $this->dbData['db']);

    if(!$erg) {
      throw new Exception("DB-Verbindung fehlgeschalgen!");
    }

    $sql = "
            SET NAMES
              utf8mb4
            ";

    $this->query($sql);

    $sql = "
            SET CHARACTER SET
              utf8mb4
            ";

    $this->query($sql);

    $sql = "
            SET SESSION
              sql_mode = ''
            ";

    $this->query($sql);

    self::$connections[] = $this;
  }

  public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
    if(!@$this->ping()) {
      $this->close();
      $this->dbConnect();
    }

    try {
      $mt = microtime(true);
      $res = parent::query($query, $resultmode);
      $mt2 = microtime(true);

      $sek = $mt2-$mt;

      $this->countQueries++;
      $this->queryLog[] = array(
        "query" => $query,
        "time"  => $sek
      );

      if($this->errno) {
        if(in_array($this->errno, array(1205, 1213))) {
          // Lock wait timeout exceeded; try restarting transaction
          // Deadlock --> Abfrage nochmal senden
          $this->query($query, $resultmode);
        } else {
          ob_start();
          var_dump($_SERVER);
          debug_print_backtrace();
          $c = ob_get_contents();
          ob_end_clean();
          sendMail("xxxxx@xxxxx.de", "SQL-Error ".$this->application, "ErrNo: ".$this->errno.PHP_EOL."Error: ".$this->error.PHP_EOL.PHP_EOL.$query.PHP_EOL.PHP_EOL.$sek." Sekunden".PHP_EOL.$c);
        }
      }

      return $res;
    } catch(Exception $e) {
      sendMail("xxxx@xxxxx.de", "DB nicht erreichbar", $this->dbData['host']." - ".$this->dbData['db']);
      return null;
    }
  }

  public static function getConnection($application) {
    if(!isset(self::$connections[$application])) {
      $db = new Database($application);

      self::$connections[$application] = $db;
    }

    return self::$connections[$application];
  }

  public function __call($methode, $args) {
    var_dump($methode, $args);
    exit;
  }

  public function getQueriesCount() {
    return $this->countQueries;
  }

  public function getQueryLog() {
    return $this->queryLog;
  }

  public function getLastQueryTime() {
    return $this->lastQueryTime;
  }
}

function getDBConnect($anwendung) {
  try {
    $db = Database::getConnection($anwendung);
  } catch(Exception $e) {
    ob_start();
    var_dump($e);
    $c = ob_get_contents();
    ob_end_clean();
    mail("xx@xxxxxx.de", "Cronjob: DB ".$anwendung." nicht erreichbar", $c);
    return null;
  }

  return $db;
}

这是在脚本上:

  $db2 = getDBConnect("tbde");
  $sql = "
      SELECT
        s.sURL, s.sName, s.amz, IF(s.gesperrt = 1 OR sp.gesperrt = 
 1, 1, 0) gesperrt,
        sp.id, sp.shpID, sp.partnerSName
      FROM
        s_partner sp
      LEFT JOIN
        shp s ON(s.id = sp.shpID)
      WHERE
        sp.partnerSID = ".intval($row['partnerID'])." AND
        sp.partner = 'E'
      "; 
$res2 = $db2->query($sql);

标签: phpfatal-error

解决方案


推荐阅读