Support ⇒ Explain Please :: Archives ⇒ Checking a table exists :: Archived ⇒ Community Forums ⇒ CPG Dragonfly™ CMS
Forum IndexExplain Please

Archived ⇒ Checking a table exists


What's the best way of checking whether a table exists in the database? Is there a function for it in $db, or is there an SQL statement I can use to check, or do I simply use a CREATE TABLE IF NOT EXISTS statement to create the table if it's not there?

This is for something I want to release for free by the way, not a commercial module.

Gaming League / Cup - www.leaguecms.co.uk :: Other DragonFly modules - www.cmsdreams.co.uk

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):


echo '<pre>'; print_r($db->list_tables()); echo '</pre>';

.:: I met php the 03 December 2003 :: Unforgettable day! ::.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
CloudLinux / Apache 2.4 LSAPI / MySQLi 5.6 / PHP 5.6 / DCVS


Had to solve this one myself recently. There is very little info online (beyond Nano's suggestion - which seems sledgehammer-to-crack-a-walnut for just 1 table).

Settled on:

$result = $db->sql_query("SHOW TABLES LIKE '".$prefix."\_%\_forums'");

Obviously put whatever you need in the like string, but I left mine in as an example.

Pro_News CM™ - Content Management for Dragonfly CMS™

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux / 1.3.39 - 2.4.9 / 5.5.42 - 5.6.16 / 5.4.37 - 5.5.11 / 9.4


I've used elsewhere,$tableexists = $db->sql_query("DESCRIBE ".$prefix."_sometable");

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):

All times are UTC