How to get all table names in yii?
如何获取yii中的所有表名?
The sql query in mySQL is SHOW TABLES
. I tried:
mySQL中的sql查询是SHOW TABLES。我试过了:
$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
3 个解决方案
#1
6
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
请参阅:如何从Yii Framework中的数据库中获取所有表名和列名
#2
3
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
如果你想使用yii,那么Mani的答案是正确的。如果您想使用create Command获取表名,那么您可以使用
$sql='SELECT * FROM INFORMATION_SCHEMA.TABLES'
$tables = Yii::app()->db
->createCommand($sql)
->queryAll();
#3
0
The simple is simple:
简单很简单:
var_dump(Yii::app()->db->schema->getTableNames();
http://www.yiiframework.com/doc/api/1.1/CDbSchema - here look for detail
http://www.yiiframework.com/doc/api/1.1/CDbSchema - 这里寻找细节
#1
6
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
请参阅:如何从Yii Framework中的数据库中获取所有表名和列名
#2
3
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
如果你想使用yii,那么Mani的答案是正确的。如果您想使用create Command获取表名,那么您可以使用
$sql='SELECT * FROM INFORMATION_SCHEMA.TABLES'
$tables = Yii::app()->db
->createCommand($sql)
->queryAll();
#3
0
The simple is simple:
简单很简单:
var_dump(Yii::app()->db->schema->getTableNames();
http://www.yiiframework.com/doc/api/1.1/CDbSchema - here look for detail
http://www.yiiframework.com/doc/api/1.1/CDbSchema - 这里寻找细节