Listing out Database Table Names

December 28th, 2009 § 0

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 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

What's this?

You are currently reading Listing out Database Table Names at SilentGap.

meta