Jul 21, 2015

Connect MySQL Remotely, Amazon EC2 using MySQL Workbench

Login to AWS Management Console. Under the security group, add inbound rule for MySQL.

First login to EC2 instance using SSH, then login to mysql 
mysql -hlocalhost -uroot -p
provide the password. Once you are in mysql prompt
CREATE USER 'testuser'@'%' IDENTIFIED BY 'testpwd'
GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'%' WITH GRANT OPTION
FLUSH PRIVILEGES
Note: Setting host to '%' may be harmful, you can set your IP address to access the MySQL from your IP address only

Now open the my.cnf file from /etc/mysql and search for bind-address, the default value will be
bind-address = 127.0.0.1
change it to
 bind-address = 0.0.0.0
That's it now go to MySQL workbench, create connection with
Host:
Username: testuser
 

Jul 2, 2015

MySQL Simple JOINS



Simple Example: Lets say you have a Students table, and a Lockers table.
Each student can be assigned to a locker, so there is a "Locker Number" column in the student table. More than one student could potentially be in a single locker, but especially at the beginning of the school year, you may have some incoming students without lockers and some lockers that have no students assigned.
For the sake of this example, lets say you have 100 students, 70 of which have lockers. You have a total of 50 lockers, 40 of which have at least 1 student and 10 lockers have no student.
INNER JOIN is equivalent to "show me all students with lockers".
Any students without lockers, or any lockers without students are missing.
Returns 70 rows
LEFT OUTER JOIN would be "show me all students, with their corresponding locker if they have one".
This might be a general student list, or could be used to identify students with no locker.
Returns 100 rows
RIGHT OUTER JOIN would be "show me all lockers, and the students assigned to them if there are any".
This could be used to identify lockers that have no students assigned, or lockers that have too many students.
Returns 80 rows (list of 70 students in the 40 lockers, plus the 10 lockers with no student)
FULL OUTER JOIN would be silly and probably not much use.
Something like "show me all students and all lockers, and match them up where you can"
Returns 110 rows (all 100 students, including those without lockers. Plus the 10 lockers with no student)
CROSS JOIN is also fairly silly in this scenario.
It doesn't use the linked "locker number" field in the students table, so you basically end up with a big giant list of every possible student-to-locker pairing, whether or not it actually exists.
Returns 5000 rows (100 students x 50 lockers). Could be useful (with filtering) as a starting point to match up the new students with the empty lockers.
Original here

Jan 17, 2015

Find Time Ago using PHP for given date time

If you want to display time like one minute ago, 10 minutes ago .. etc. Below is the method that can be used, need to pass DateTime in YYYY-MM-DD HH:II:SS format.


Method One:
/**
     * returns the time ago in string
     *
     * @param string $date_time           
     * @return string
     */
    function timeAgo($date_time)
    {
        $time_ago = strtotime($date_time);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
       
        $str = '';
        // Seconds
        if ($seconds <= 60) {
            $str = "few seconds ago";
        }  // Minutes
else
            if ($minutes <= 60) {
                if ($minutes == 1) {
                    $str = "one minute ago";
                } else {
                    $str = "$minutes minutes ago";
                }
            }  // Hours
else
                if ($hours <= 24) {
                    if ($hours == 1) {
                        $str = "an hour ago";
                    } else {
                        $str = "$hours hours ago";
                    }
                }  // Days
else
                    if ($days <= 7) {
                        if ($days == 1) {
                            $str = "yesterday";
                        } else {
                            $str = "$days days ago";
                        }
                    }  // Weeks
else
                        if ($weeks <= 4.3) {
                            if ($weeks == 1) {
                                $str = "a week ago";
                            } else {
                                $str = "$weeks weeks ago";
                            }
                        }  // Months
else
                            if ($months <= 12) {
                                if ($months == 1) {
                                    $str = "a month ago";
                                } else {
                                    $str = "$months months ago";
                                }
                            }  // Years
else {
                                if ($years == 1) {
                                    $str = "one year ago";
                                } else {
                                    $str = "$years years ago";
                                }
                            }
        return $str;
    }
 ex:
echo timeAgo('2015-01-17 14:20:00');
o/p: 31 minutes ago
 
Method Two: in detail
/**
     * gives time ago in detailed
     *
     * @param string $date_time           
     * @return string
     */
    function timeAgo($date_time)
    {
        $then = new DateTime($date_time);
        $now = new DateTime();
        $delta = $now->diff($then);
       
        $quantities = array(
            'year' => $delta->y,
            'month' => $delta->m,
            'day' => $delta->d,
            'hour' => $delta->h,
            'minute' => $delta->i,
            'second' => $delta->s
        );
       
        $str = '';
        foreach ($quantities as $unit => $value) {
            if ($value == 0)
                continue;
            $str .= $value . ' ' . $unit;
            if ($value != 1) {
                $str .= 's';
            }
            $str .= ', ';
        }
        $str = $str == '' ? 'a moment ' : substr($str, 0, - 2);
        return $str . ' ago';
    }
 ex:
echo timeAgo('2015-01-17 14:20:00');
o/p: 31 minutes, 14 seconds ago