使用codeigniter在mysql数据库中创建表

时间:2015-08-22 07:58:03

标签: php mysql codeigniter

我尝试使用以下代码在mysql DB中创建一个带有codeigniter的表:

$posts_fields=array(
        'id'=>array('type' => 'INT','constraint' => 5,'unsigned' => TRUE),
        'title'=>array('type' =>'VARCHAR','constraint' => 100),
        'content'=>array('type'=>'text'),
        'create_time'=>array('type'=>'INT','constraint'=>12));

    $this->dbforge->add_field($posts_fields);
    $this->dbforge->create_table('posts');

但是任何时候都会收到错误"没有选择数据库"。

如何选择数据库来创建表格? (此数据库不是默认数据库)

1 个答案:

答案 0 :(得分:2)

您可以查看Codeigniter showing error: No database selected

如果您使用的是其他数据库,请使用以下内容

$this->db->query('use db2');

然后把你的代码。