Get all table names in yii

痴心易碎 提交于 2020-05-15 04:04:07

问题


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 

回答1:


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




回答2:


If you want to use yii then Mani`s answer is correct. If you want to get table names using create Command then you can use

$sql='SELECT * FROM INFORMATION_SCHEMA.TABLES'
$tables = Yii::app()->db
         ->createCommand($sql)
         ->queryAll();



回答3:


The simple is simple:

var_dump(Yii::app()->db->schema->getTableNames();

http://www.yiiframework.com/doc/api/1.1/CDbSchema - here look for detail



来源:https://stackoverflow.com/questions/21981355/get-all-table-names-in-yii

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!