admin管理员组

文章数量:1278910

The file path is: wordpress/​wp-content/​themes/​neve/​index.php

"index.php" content is:

<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM users WHERE username = 'Peter'");
foreach ( $result as $results )
{
    echo $results->id.'<br/>';
    echo $results->username.'<br/>';
    echo $results->password.'<br/>';
}
?>

It works! But, when I change the content of the index.php, see below: New "index.php" content is:

<?php
header("Location: http://34.92.209.11/wp-content/themes/neve/Test_PHP.php");
?>

Then the content of the "Test_PHP.php" is:

<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM users WHERE username = 'Peter'");
foreach ( $result as $results )
{
    echo $results->id.'<br/>';
    echo $results->username.'<br/>';
    echo $results->password.'<br/>';
}
?>

It does not work, and shows "HTTP ERROR 500", why??? Only the file name called "index.php" can access the database? I've been trying to find the answer. I really hope someone can answer me. Thank you so much!

The file path is: wordpress/​wp-content/​themes/​neve/​index.php

"index.php" content is:

<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM users WHERE username = 'Peter'");
foreach ( $result as $results )
{
    echo $results->id.'<br/>';
    echo $results->username.'<br/>';
    echo $results->password.'<br/>';
}
?>

It works! But, when I change the content of the index.php, see below: New "index.php" content is:

<?php
header("Location: http://34.92.209.11/wp-content/themes/neve/Test_PHP.php");
?>

Then the content of the "Test_PHP.php" is:

<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM users WHERE username = 'Peter'");
foreach ( $result as $results )
{
    echo $results->id.'<br/>';
    echo $results->username.'<br/>';
    echo $results->password.'<br/>';
}
?>

It does not work, and shows "HTTP ERROR 500", why??? Only the file name called "index.php" can access the database? I've been trying to find the answer. I really hope someone can answer me. Thank you so much!

Share Improve this question edited Nov 15, 2021 at 23:21 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Nov 15, 2021 at 22:50 Flicker LauFlicker Lau 11 bronze badge 2
  • 2 Is this the full contents of the files? Is there a reference to get_header() which would contain wp_head() to give you access to $wpdb? Could you remove the image and add in actual, workable code, please? – Howdy_McGee Commented Nov 15, 2021 at 23:07
  • 1 I've edited your question to restore the original edit and then wrapped the code in code blocks as there was no formatting. Do not upload images of text – Tom J Nowell Commented Nov 15, 2021 at 23:20
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: phpPlease help I have a problem with getting wordpress databas