PHP&;WP:try-and-catch上是否存在错误
我发布了以下问题。目前,我正在尝试防止数据库为只读时应用程序崩溃 目前,我发现以下错误: WordPress数据库错误插入命令拒绝给用户 “readonly”@“10.XXX.XX.XX”,用于查询插入到的表“responses”PHP&;WP:try-and-catch上是否存在错误,php,mysql,wordpress,Php,Mysql,Wordpress,我发布了以下问题。目前,我正在尝试防止数据库为只读时应用程序崩溃 目前,我发现以下错误: WordPress数据库错误插入命令拒绝给用户 “readonly”@“10.XXX.XX.XX”,用于查询插入到的表“responses” 响应(uid,数据)值 当具有只读访问权限的数据库试图向其中插入数据时,就会发生这种情况。在集群故障转移过程中,writer数据库成为读卡器,因此发生了此问题。我试图为它编写错误处理,但没有成功 function drools_request($data, $uid)
响应
(uid
,数据
)值
当具有只读访问权限的数据库试图向其中插入数据时,就会发生这种情况。在集群故障转移过程中,writer数据库成为读卡器,因此发生了此问题。我试图为它编写错误处理,但没有成功
function drools_request($data, $uid) {
try {
$db = _get_db();
$insertion = $db->insert("requests", [
"uid" => $uid,
"data" => json_encode($data),
]);
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
}
catch(\Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
变量$db
是来自AWS RDS端点的数据库,该端点具有--只读访问权限。所以当$db->insert
抛出一个错误时,我希望WP引擎上的日志显示'error writing to database:'
但是,这并没有发生,我看到的是WordPress database error insert命令被拒绝给用户…
为什么在这种情况下错误处理不起作用?无法写入数据库不应阻止网站工作。这是全班同学
<?php
namespace StatCollector;
function drools_request($data, $uid) {
try {
$db = _get_db();
$insertion = $db->insert("requests", [
"uid" => $uid,
"data" => json_encode($data),
]);
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
}
catch(\Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
function drools_response($response, $uid) {
try {
$db = _get_db();
$insertion = $db->insert("responses", [
"uid" => $uid,
"data" => json_encode($response),
]);
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
}
catch(\Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
function results_sent($type, $to, $uid, $url = null, $message = null) {
try {
$db = _get_db();
$insertion = $db->insert("messages", [
"uid" => $uid,
"msg_type" => strtolower($type),
"address" => $to,
"url" => $url,
"message" => $message
]);
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
}
catch(\Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
function peu_data($staff, $client, $uid) {
try {
if (empty($uid)) {
return;
}
$db = _get_db();
if (! empty($staff)) {
$insertion = $db->insert("peu_staff", [
"uid" => $uid,
"data" => json_encode($staff)
]);
}
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
if (! empty($client)) {
$insertion = $db->insert("peu_client", [
"uid" => $uid,
"data" => json_encode($client)
]);
}
if( is_wp_error( $insertion ) ) {
echo $return->get_error_message();
}
}
catch(\Exception $e){
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
function response_update() {
$uid = $_POST['GUID'];
$url = $_POST['url'];
$programs = $_POST['programs'];
if (empty($uid) || empty($url) || empty($programs)) {
wp_send_json(["status" => "fail","message" => "missing values"]);
return wp_die();
}
try {
$db = _get_db();
$insertion = $db->insert("response_update", [
"uid" => $uid,
"url" => $url,
"program_codes" => $programs
]);
wp_send_json(["status" => "ok"]);
wp_die();
if( is_wp_error( $insertion )) {
echo $return->get_error_message();
}
}
catch(\Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
function _get_db() {
$host = get_option('statc_host');
$database = get_option('statc_database');
$user = get_option('statc_user');
$password = get_option('statc_password');
$bootstrapped = get_option('statc_bootstrapped');
$host = (!empty($host)) ? $host : $_ENV['STATC_HOST'];
$database = (!empty($database)) ? $database : $_ENV['STATC_DATABASE'];
$user = (!empty($user)) ? $user : $_ENV['STATC_USER'];
$password = (!empty($password)) ? $password : $_ENV['STATC_PASSWORD'];
$bootstrapped = (!empty($bootstrapped)) ? $bootstrapped : $_ENV['STATC_BOOTSTRAPPED'];
if (empty($host) || empty($database) || empty($user) || empty($password)) {
error_log('StatCollector is missing database connection information. Cannot log');
return new MockDatabase();
}
$db = new \wpdb($user, $password, $database, $host);
$db->show_errors();
if ($bootstrapped !== '5') {
__bootstrap($db);
}
return $db;
}
因为在wp_db
类上插入不会在失败时抛出错误
它只是返回false。try..catch
块将捕获抛出的错误
在这种情况下,您只需检查插入的结果是否为==false
请不要多次发布同一问题。编辑原始问题和所有更新。这是一个完全不同的问题。这与第一篇文章中发布的try
和catch
无关。这是关于wp\u错误
未引发错误。我强烈反对编辑第一个问题,第一个问题的答案是正确的,但不是解决整个问题的方法。重新打开问题。