CodeIgniter for Oracle: ADOdb Database Abstraction vs CodeIgniter Database Library

Share

Early last year, I got the chance to learn and develop a huge web application using CodeIgniter PHP Framework. It was my first project using the framework so at that time we have decided to avoid using CodeIgniter default Database Library in fear of incompatibility with oci8 or Oracle10g.

Only afterward did I got a chance to test out CodeIgniter Database Library or ActiveRecord Library on project using MySQL. At this point, the simplicity of ActiveRecord really caught my attention but how would it be enough compared to ADOdb when using Oracle Database?
[Click here to read more...]

Comparing Today Date in MySQL

Share

mysql
Conventionally getting today date from database in the past would require the use of date('Y-m-d'); function and add it to MySQL query such as below:

$today_date = date('Y-m-d');
$tmrw_date = date('Y-m-d', strtotime("+1 day"));
$sql = "SELECT * FROM my_table
    WHERE my_datetime BETWEEN '" . $today_date . "' AND '" . $tmrw_date . "'";

However this method might return false positive result if the web server and database server doesn’t have it date and time synchronized. From my point of view, it best to let the database server have the full control to insert, update or compare the dates. So what the alternative? You can check the full specification from MySQL Date and Time Functions but I would opt for DATEDIFF if you specifically want to compare with today date.

SELECT * FROM my_table
    WHERE DATEDIFF(my_datetime, SYSDATE())=0

Database Table Prefix in CodeIgniter

Share

I must say one of the main reason why I started to look into ActiveRecord for CodeIgniter was the availability to use database table name prefix.

|    ['dbprefix'] You can add an optional prefix, which will be added
|                 to the table name when using the  Active Record class
$db['default']['dbprefix'] = "feed_";

For example: TABLE feed_item

$this->db->get('item');

Many might not know that you can also use it with normal database query using 'swap_pre'. In ./system/application/config/database.php

|    ['dbprefix'] You can add an optional prefix, which will be added
|                 to the table name when using the  Active Record class
$db['default']['dbprefix'] = "feed_";
$db['default']['swap_pre'] = "{PRE}";

So now you can actually use.

$sql = "SELECT * FROM {PRE}item";
$query = $this->db->query($sql);

UPDATE on SELECT With MySQL

Share

mysql
Previously if there a need to something like this, I would have definitely create a temporary tool using PHP to automatically loop a SELECT and UPDATE each field inside the loop, for example:

$sql = "SELECT * FROM item";
$query = $this->db->query($sql);
foreach( $query->result_array() as $row ) :
    $this->db->query("UPDATE relationship
        SET rel_date=?
        WHERE (rel_value=? AND rel_type=1)", array(
        $row['item_datetime'],
        $row['item_id']
    ));
endforeach;

This actually can be simplify using only MySQL query such as:

UPDATE relationship r, item i
    SET r.rel_date=i.item_datetime
    WHERE (r.rel_value=i.item_id AND r.rel_type=1)

Install Apache2, MySQL5, PHP5 and PHPMyAdmin on Ubuntu

Share

This is a collection of guide how to install Apache Httpd Web Server complete with latest PHP (as Apache mod), MySQL for Ubuntu. As a bonus, I even through in an additional guide to install PHPMyAdmin. Please do note that most of this guide can be found elsewhere which is listed at the bottom of the guide.

[Click here to read more...]