Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all table names in yii

How to get all table names in yii?

The sql query in mySQL is SHOW TABLES. I tried:

$sql = 'SHOW TABLES';
$tables = Yii::app()->db
         ->createCommand($sql)
         ->queryAll();
print_r($tables);

It throws an error:

CDbCommand failed to execute the SQL statement:
CDbCommand failed to prepare the SQL statement:
SQLSTATE[HY000]: General error: 1 near "SHOW": syntax error.
The SQL statement executed was: SHOW TABLES 
like image 769
Mohammed H Avatar asked Dec 25 '22 13:12

Mohammed H


1 Answers

try this one:

$connection = Yii::app()->db;//get connection
$dbSchema = $connection->schema;
//or $connection->getSchema();
$tables = $dbSchema->getTables();//returns array of tbl schema's
foreach($tables as $tbl)
{
    echo $tbl->rawName, ':<br/>', implode(', ', $tbl->columnNames), '<br/>';
}

Refer:How to get all table and column names from database in Yii Framework

like image 122
Mani Avatar answered Dec 28 '22 09:12

Mani