0

I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")

I have been researching for 2 days but nothing works I tried.

In my page; I use the php code shown in blow.

<?php
global $wpdb;
   $words = $wpdb->get_results("SELECT * FROM $wpdb->words")

echo $words[0]->ENG;


?>

I wonder that; - Which directory does my php page to be into ? - What I need to do (other config, permission etc.) to do what I want.

Regards.

Tugrul
  • 1,760
  • 4
  • 24
  • 39

1 Answers1

1

If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.

I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.

EDIT

Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try

$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")

instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.

Community
  • 1
  • 1
Hobo
  • 7,536
  • 5
  • 40
  • 50
  • I am loading it in the theme. I can reach posts, users table and I can write some of columns of this tables using the query like shown in my question. I can not select the data which is into mycustom table named wp_words. Is there any way to reach custom tables in wp ? – Tugrul Aug 25 '13 at 11:48
  • Really thanks. Your answer solved my problem. That answer, too. --> http://stackoverflow.com/a/3387110/1172945 – Tugrul Aug 25 '13 at 15:53