如何在zend中获取数据库中表的列名? [英] How to get the column names of table in a database in zend?

查看:42
本文介绍了如何在zend中获取数据库中表的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在zend中创建一个应用程序,用户将在其中输入主机,数据库名称,用户名和密码来连接数据库,并能够检索表中的表和列,从而开始一些工作.

I am creating an app in zend where the user will enter the host, database name, username and password to connect the database and will able to retrieve the tables and columns in the tables also to start some work..

我正在使用以下代码来获取数据库中的表:

I am using this code to get the tables in a database:

$ExternalDb = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => $host,
    'username' => $user,
    'password' => $pass,
    'dbname'   => $dbName
));

try{
    //Create connection
    echo $ExternalDb->getConnection()->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);

    foreach($ExternalDb->listTables() as $table){

      //$table = new $table(array('db' => $ExternalDb)); it doesn't work
        echo "<pre>";
        print_r($table);
      //$cols = $table->info(Zend_Db_Table_Abstract::COLS); It doesn't work
        echo "</pre>";

    }

} catch (Exception $ex) {
    echo $ex;
}

我能够获取表名,但是我也在尝试获取列名...

I am able to get the table names but I am trying to get the column names also...

提前谢谢!

推荐答案

您可以从Zend_Db_Table_Abstract检索信息.

创建一个像这样的新类:

Create a new class like this:

class foo extends Zend_Db_Table_Abstract
{
}

在您的代码中:

foreach($ExternalDb->listTables() as $table){
    $dbTable = new foo($ExternalDb);            
    $dbTable->setOptions(array($dbTable::NAME => $table));
    var_dump($dbTable->info($dbTable::COLS));
    unset($dbTable);
}

这篇关于如何在zend中获取数据库中表的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆