So I have an html element where the text reflects a text field in my database, this works normally. I need it to update every so often so I have:

    }, 1000);

Which just grabs the echo from load_marque.php.

Inside of load_marque.php I simply have:

global $wpdb;

$Text = "Test Text";
echo $Text;

It works fine. And if I change the text in $Text then update, it also update in the html element.

But when I then try to pull from my database within load_marque.php it seems to break for some reason.


global $wpdb;
$marque_text = $wpdb->get_row ( "SELECT * FROM marque_text WHERE ID = '1' ", OBJECT );
$Text = $marque_text->Text;
echo $Text;

The html element text is blank.

I am positive my query is correct.

I've been banging my head trying to figure this out. Can I NOT access my database using an outside php script?

Even when I just add in the line:

$marque_text = $wpdb->get_row ( "SELECT * FROM marque_text WHERE ID = '1' ", OBJECT );

and still have

$Text = "Test Text";
echo $Text;

at the end, it comes out blank;

So its breaking just before $Test = "Test Text";


Initial thoughts are that it depends where your load_marque.php file is located. I assume within a theme or plugin.

If so, what happens when the file is called directly. If there are some errors, these will be shown on the page.

Based on the on here https://codex.wordpress.org/Class_Reference/wpdb your query your query may need to be changed slightly. An example from the codex shows it more like this:

global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT ); 

The differences seem to center around not requiring the ID number to be wrapped in single quote marks.

  • The php file is located in the same folder as the main php file, which are located in the templates folder. – Dave DF Apr 11 '18 at 20:37
  • Tried that still seems to not work. I've even tried $results = $GLOBALS['wpdb']->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT ); – Dave DF Apr 11 '18 at 20:42
  • I think EBennett is on the right track - it sounds like your PHP file is inside of your theme, but since it's a standalone file, you need to add a bit of code to enable WordPress functions within that file. Theme files work because WP is loaded in header.php but your standalone isn't working because WP is never called. See: wordpress.stackexchange.com/questions/47049/… – WebElaine Apr 11 '18 at 21:05
  • I will look into that. Just so you know: The page that is calling load_marque.php also resides in the same folder, and I am accessing my database all over the place with no issues. If I copy and paste //$marque_text = $wpdb->get_row ( "SELECT * FROM marque_text WHERE ID = '1' ", OBJECT ); I have no problems using $results->Text; – Dave DF Apr 11 '18 at 21:10
  • I'm assuming by templates folder you mean your theme. If there are no errors appearing, it may be because whatever field you are trying to access doesn't contain any data? Another solution, maybe to switch your Javascript call into an AJAX request, passing in the full wp themes path. That way you can have your PHP script return your results as json and then update the HTML elements that way. – EBennett Apr 11 '18 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.