Comparing Today Date in MySQL

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

UPDATE on SELECT With MySQL

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

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...]

The Making Of FeedMalaya

promo-logo

FeedMalaya is a social blog aggregator that pull out the best content from Malaysian bloggers was launched on June 7th, 2009 developed by Bat InfoMalaya and myself.

[Click here to read more...]

Combining Multiple Table Columns in MySQL

Question

How to combine address1, address2, address3 and address4 in the following table to a single column such as address?

CREATE TABLE user (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`address1` VARCHAR( 100 ) NOT NULL ,
`address2` VARCHAR( 100 ) NOT NULL ,
`address3` VARCHAR( 100 ) NOT NULL ,
`address4` VARCHAR( 100 ) NOT NULL ,
`postcode` VARCHAR( 6 ) NOT NULL ,
`state` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM;

[Click here to read more...]