Listing out Database Table Names

Ok, for WordPress & WPMU, let me preface this post with saying standard WordPress tables can be found by using the $wpdb object (paste the below code and you might be surprised on what’s available to you).

echo "<pre>"; print_r($wpdb); echo "</pre>";

But, let’s say the plugin you’re working with in WordPress isn’t in the $wpdb object & you still need to grab a list of non-standard tables, I’ve been using the below code to grab the information I need.

class BlogTables {
	function selectNames() {
		global $wpdb;

		$prefix	        = "qdf_"; // other non $table_prefix prefixes
		$sql		= "SHOW TABLES LIKE '".$prefix."%'";
		$avail_tables 	= $wpdb->get_results($sql);

		foreach ($avail_tables as $tables) {
			foreach($tables as $key => $table){
				$blog_tables[] = $table;
			}
		}
		return $blog_tables;
	}
}

You can grab the dirty output from the above code with

$tables_avail = new BlogTables;
$tables_avail->selectNames();

From there, you should be able to use it for whatever it is you need to use it for. If you have a better way of doing things, I’d be interested in knowing.

This entry was posted in Code, Development, Web, WordPress. Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>