php - 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);
解决方案
推荐阅读
- c# - Angular 6 - 对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头
- uwp - MyApp_language-xx.appx 不会为适用于商店的 Windows 10 程序包生成
- asp.net-mvc - 我们如何使用 MVC 访问服务器端的 sessionStorage?
- docker - wurstmeister/kafka 要绑定的卷?- 码头工人
- svg - 带有视口的 SVG 在 Chrome 中无法缩放
- tensorflow - Keras 模型 OOM 在训练期间死亡
- javascript - 将 webcomponents 捆绑到 npm 包中,以便可以选择性地导入 webcomponents
- javascript - 如何确定元素是否是数字,甚至使用条件
- three.js - 如何使用 arjs 源类型将 3D 对象固定在场景中:网络摄像头
- css - BS4 - 向右浮动第一列