Tuesday, January 27, 2015

How Fo Fix .htaccess Problem In Codeigniter on Ubuntu 14.04 LAMP Stack Server?

All was working nicely in the localhost. But when I transfered the project on the Ubuntu 14.04 LAMP Stack Server, I was unable to access the .htaccess file in my projects root. Whenever I try to serve a page, I get a 404 error. After some research I found out the solution to fix it.

/* Preliminary steps to make sure the correct config in CI */

1. Make .htaccess file in your CI root directory using below code

   RewriteEngine on
   RewriteCond $1 !^(index\.php|assets|robots\.txt)
   RewriteRule ^(.*)$ index.php?/$1 [L]

2. Check /application/config/config.php and set $config['index_page'] = "";

3. make sure that mod_rewrite is on your server. Use phpinfo(), if you are not sure.


After enabling mod_rewrite, we need to change setting of AllowOverride to All
/* Change the apache2 configuration to enable override all */

1. Find your apache2.conf, in my case, it's in /etc/apache2/

2. So open it and change the expected feature:

   Find and change on <directory  /var/www/>
     AllowOverride none
     to
     AllowOverride All

3. Restart your apache to make sure new changes is activated. I did via terminal as below.

     root@example.se:~# service apache2 restart 

And it's working. :)

Using SSH To Import MySQL Database via Terminal

The file must be in .sql format. It can not be compressed in a .zip or .tar.gz file.
1. Upload the SQL file to the server eg. file_import.sql.
2. If the database does not exist please create one with an user. Note the username and password.
3. Log into the server through SSH
4. Type this this command: mysql -pPassword database_name < file_import.sql

I have wrote detail steps about how I successfully imported the mysql database using SSH via terminal. I have tested in Server version: 5.5.41-0ubuntu0.14.04.1 (Ubuntu).
/* Terminal example*/

Anup-2:~ anup$ ssh root@example.se  //initializing SSH connection 
root@example.se.se's password:****** //Enter password to log in to server
root@example.se:~# mysql -u root -p //Command to login to mysql server 
Enter password: *****  //Enter password to log in to mysql server

/* ... You will get welcome message 
.......... Last login: Mon Jan 26 15:27:35 2015 from ..... */

mysql> show databases; // to list all the databases
mysql> exit;  //exit to root server
Bye
root@example.se:#  cd /

root@example.se:/# ls // list all the files inside root directory 

root@dmsstage:/# mysql -pPassword database_name < file_import.sql  //command to import your mysql database (Place the password immediately after -p)

root@dmsstage:/# mysql -pPassword // login to mysql to verify if the database has been imported
mysql> use database_name; //command to select database;
Database changed
mysql> show tables; //command to list all the tables inside the database

Monday, January 19, 2015

How To Clone a Row in MySQL Using Codeigniter?

If you've ever needed to clone or duplicate a MySQL row or record with a unique ID field, here is a clean and simple Codeigniter function. It clones the field names and the row values and then insert datas into table with unique primary key.
function DuplicateMySQLRecord ($table, $primary_key_field, $primary_key_val) 
{
   /* generate the select query */
   $this->db->where($primary_key_field, $primary_key_val); 
   $query = $this->db->get($table);
  
    foreach ($query->result() as $row){   
       foreach($row as $key=>$val){        
          if($key != $primary_key_field){ 
          /* $this->db->set can be used instead of passing a data array directly to the insert or update functions */
          $this->db->set($key, $val);               
          }//endif              
       }//endforeach
    }//endforeach

    /* insert the new record into table*/
    return $this->db->insert($table); 
}