Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

what is the correct way to query the a Wordpress database, meaning to pull row and column info, and then place it into a corresponding table in the Wordpress admin?

I'm wanting to do a query similar to that of the "links" section in the Wordpress admin, being able to both "edit" and "delete" the rows/columns individually.

I've already found plenty of articles on setting up tables, and have the edit/delete front end in place, but just need a solid example of how to make a database query pull the info to edit/delete/add the info to the table.

share|improve this question
    
use wpdb to query database, read more about it in codex codex.wordpress.org/Class_Reference/wpdb –  Eugene Manuilov Jun 4 '12 at 20:15
    
Thanks Eugene, the codex has definitely helped, I guess it's difficult for me because I need the query to return results into a table format, so I'm stumped. In other words, how can I format the query to output results in a table format? –  Rob Myrick Jun 4 '12 at 20:28

1 Answer 1

up vote 1 down vote accepted

Querying in WordPress is done by using the the wpdb variable. Be sure to always prepare your sql so WP can make it database safe.

Example:

global $wpdb;
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}TABLE_NAME` WHERE `var` = %d", 1) );

Then you can loop through the data and output it as for example a table (table formatting not included).

foreach($rows as $row) 
{
  echo $row->post_title;
}

Please note that the SQL query above doesn't work because it contains dummy data. Please change it so it fits your needs.

share|improve this answer
    
OK, Barry the idea makes sense......so echo $row->post_title; would take care of the row display only, or would that also display the columns as I need? –  Rob Myrick Jun 4 '12 at 20:59
    
The echo $row->post_title; would only display the title of the post. If you would like to display a row with a single column that would display the title, you can do echo "<tr><td>{$row->post_title}</td></tr>\n";. –  Barry Kooij Jun 4 '12 at 21:02
    
Thanks, Barry..... –  Rob Myrick Jun 4 '12 at 21:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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