views:

30

answers:

1

I am trying to pull data from a table I have imported into my wordpress database. My php code works for all the default wp_ tables but when try and target the tables I actually want I get bugger all back.

My code (currently echoing all post titles and it works)

$liveposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts
 WHERE post_status = 'publish'") );

 foreach ($liveposts as $livepost) {
  echo '<p>' .$livepost->post_title. '</p>';
}

I imported 3 tables from another database, and yes they do have data in to pull out. I found out that the $wpdb->posts expects the posts table to be wp_posts.. so I tried renaming my tables to wp_bus_route... but still nothing.

I used myphpadmin to export 3 tables out of a large database (in a .sql format) and imported them. i can see the tables in myphpadmin and view all the data in them.

This is my 1st time pulling data from the wp database so i ma of missed something obvious. any pointers please :)

+1  A: 

Try to not using variables at table names:

$liveposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_bus_route
 WHERE post_status = 'publish'") );
antyrat
Cool, that has done the trick thanks :) Any thoughts on why this works and $wpdb-> does not?And what is the diffence between $wpdb->bus_routes and wp_bus_routes?
Adam
It's because wordpress has $tables build in variable described in wp-includes/wp-db.php, where described alloweb tables names array( 'posts', 'comments', 'links', 'options', 'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' ); if you add to this array bus_routes it would work but whatever after wordpress updates to the newer version all your changes will be lost, so use static names of tables instead variables
antyrat
ok thanks for the insight. makes sense in my head now :)cheers guys
Adam