Search code examples
phpcodeigniterwampcodeigniter-2

Codeigniter an migration not working with wamp


I am following a tutorial and I have check the source code with my code to make sure there are no errors but I am unable to get codeigniter to create update or retrieve any records from my wamp sever(ver 2.4) when I enter public_html/admin/migration in the task bar codeigniter returns a message saying "Migration worked!" which is the result that is expected when codeigniter has updated the records but no changes are made to the database this is the code used in the controller

<?php
class Migration extends Admin_Controller
{

    public function __construct ()
    {
        parent::__construct();
    }

    public function index ()
    {
        $this->load->library('migration');
        if (! $this->migration->current()) {
            show_error($this->migration->error_string());
        }
        else {
            echo 'Migration worked!';
        }

    }
}

I have set the autoload libraries as follows $autoload['libraries'] = array('database'); is there something I am missing here is my migration library file called '001_create_users.php'

<?php

class Migration_Create_users extends CI_Migration {

    public function up()
    {
        $this->dbforge->add_field(array(
            'id' => array(
                        'type' => 'INT',
                        'constraint' => 11,
                        'unsigned' => TRUE,
                        'auto_increment' => TRUE
            ),
            'email' => array(
                        'type' => 'VARCHAR',
                        'constraint' => '100',
            ),
            'password' => array(
                        'type' => 'VARCHAR',
                        'constraint' => '128',
            ),
                        'name' => array(
                        'type' => 'VARCHAR',
                        'constraint' => '100',
            ),
        ));

        $this->dbforge->create_table('users');
    }

    public function down()
    {
        $this->dbforge->drop_table('users');
    }
}

Solution

  • Not sure if you are referring to the tutorial by Free Courses on youtube about cms buildout in codeigniter.

    The way I fixed the issue (after a few hours) was debugging down to the environment setup.

    Replace everything in your index function of the migration controller with var_dump($this->db) and see what it returns for your username/password/hostname/database, etc. If they are not what you expected per your database library setup, then your environment is not set properly in the index.php file.

    I had to fix my case statement, had an extra / or \ can not remember which one, so the switch statement was forcing it to use the production environment configuration, which were not set.