Please help! I have a problem with getting wordpress databas

In WordPress’s template hierarchy, index.php is the catch-all file for any WordPress post/page/archive/etc. that might get loaded. By the time the active theme’s index.php file is loaded, WordPress is already set up.

By redirecting from the theme’s index.php to another file, you’re re-starting the page load from scratch. If you don’t explicitly include certain WordPress files, then there are no WordPress functions, classes, etc. that will be loaded.

$wpdb, in that case, is unset (NULL, most likely), and your 500 error is most likely because you’re trying to treat an unset NULL value like an object.