douwuying4709 2015-11-02 01:40
浏览 47

在try catch中显示WPDB错误不起作用?

I am trying to create a Class that will be using a external api and wpdb class however i am having issues already in the try catch block please confirm the below is correct:

<?php
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );

class LoLApi
{
  private $database;

  public function __construct()
  {
    try {
      $this->database = new wpdb('user', 'pass', 'table', '127.0.0.1');
      $this->database->show_errors();
    } catch (Exception $e) {
      return $e->getMessage();
    }
  }

}

?>

in the other page:

<?php
  session_start();
  require 'includes/lolapi.class.php';

  $api = new LoLApi;
  exit();
?>
  • 写回答

1条回答 默认 最新

  • doulangxun7769 2016-08-06 01:53
    关注

    You should be checking instead whether it returns a WP_Error object. If the WP_Error class doesn't exist it will just die and dump an error message. Nowhere does it actually throw an exception.

    If you include wp-load.php you'll have the WP_Error class available and you can check for it in the data returned. You could also extend the wpdb class and override the db_connect method to throw a proper exception instead of killing the script on a connection failure.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。