对旨在连接到mysql数据库的PHP类进行故障排除

我正在尝试对Wordpress自定义PHP应用程序进行故障排除,问题是应该连接到数据库的Db类无法正常工作。代码如下:

{
    protected static $db = NULL;

    public static function i()
    {
        if ( static::$db === NULL )
        {
            $root = str_replace( 'dms/src/Boot.php', '', __FILE__ );

            $contents = file_get_contents( $root . 'wp-config.php' );
            $data = array();
            $socket = NULL;

            preg_match_all( '#define\([\'"](\S+?)[\'"],(\s+?)?[\'"](\S+?)[\'"]#i', $contents, $all, PREG_SET_ORDER );

            foreach( $all as $match )
            {
                $data[ $match[1] ] = $match[3];
            }

            if ( $data['DB_USER'] == 'root' )
            {
                $socket = '/Applications/MAMP/tmp/mysql/mysql.sock';
            }

            static::$db = new mysqli( "localhost", $data['DB_USER'], $data['DB_PASSWORD'], $data['DB_NAME'], 3306, $socket );

            if ( static::$db->connect_errno )
            {
                echo "Failed to connect to MySQL: (" . static::$db->connect_errno . ") " . static::$db->connect_error;
                exit();
            }
        }

        return static::$db;
    }
}

我的理解是$ data数组应使用wp-config.php文件中定义的常量DB_USER,DB_PASSWORD和DB_NAME,但似乎没有引用它。任何帮助将不胜感激。

评论