[SOLVED]To get DB table names

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
vilkis

[SOLVED]To get DB table names

Post by vilkis »

Hi,

Is there a way to get names of tables that exist in a database? As I know Adodb Lite included in CMSMS has not such a function.

vilkis
Last edited by vilkis on Tue May 26, 2009 8:53 pm, edited 1 time in total.
Duketown

Re: To get DB table names

Post by Duketown »

vilkis,

There are maybe more ways to get this done, but when you look in module AfterMM you will find the (astonishing simple) part that is as follows:

Code: Select all

		$query = "show tables";
		$dbresult = $db->Execute($query);
		$rowclass = 'row1';
		while ($dbresult && $row = $dbresult->FetchRow())
		{
			$onerow = new stdClass();
			$tablesIn = 'Tables_in_'  .  substr(cms_db_prefix(),0,strlen(cms_db_prefix()) - 1 );

			$onerow->name = $this->CreateLink($id, 'showStructure', $returnid, $row[$tablesIn], array('tablenames'=>$row[$tablesIn]));
	
			$onerow->rowclass = $rowclass;
			$entryarray[] = $onerow;
			($rowclass=="row1"?$rowclass="row2":$rowclass="row1");
		
		}
You will find this in the defaultadmin.php program.

Thanks to the author ortegaj of the module.

Duketown
vilkis

Re: To get DB table names

Post by vilkis »

Hi,
Thank you for replay.
I think it is not valid for PostgreSQL DB. I'm looking for universal tool...
I think we can press devs of core to include a full version of adodb lite... ;)
vilkis
Duketown

Re: To get DB table names

Post by Duketown »

vilkis,

I don't use PostgreSQL, but can't you use something like the following (via search of google):

Code: Select all

select * from pg_tables where tableowner = 'ME';
Duketown
alby

Re: To get DB table names

Post by alby »

In my module I use this:
global $gCms;
$qc='';
switch($gCms->config['dbms'])
{
case 'mysql':
case 'mysqli':
$qc = "SHOW TABLES";
break;
case 'postgres7':
$qc = "select table_name from information_schema.tables where table_schema='public'";
break;
}
if(empty($qc)) die('No SQL driver in config.php (mysql,mysqli,postgres7)?');

$db =& $this->GetDb();
$db->SetFetchMode(ADODB_FETCH_NUM);
$dbresult = $db->Execute($qc);
$db->SetFetchMode(ADODB_FETCH_ASSOC);
if(!$dbresult) die('Error in sql show: '.$qc);

$tables=array();
while($dbresult && $row=$dbresult->FetchRow())
$tables[] = $row[0];
Alby
vilkis

Re: To get DB table names

Post by vilkis »

Thanks for responses. Alby, I will use your code if I do not find how to implement meta module for adodb lite without hacking CMSMS.

vilkis
Post Reply

Return to “Developers Discussion”