13 Feb 2013

PHP DateTime

DateTime class is quick and dirty to get a time diff. Had to find a quick way to get diff by hours.

<?php

try{
    $datetime = new DateTime('now');
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}
 
$newdate = DateTime::createfromformat('H:i:s', '18:00:00');

try {
    $diff = $datetime->diff($newdate);
}
 catch (Exception $e) {
     echo $e->getMessage();
     exit(1);
 }

?>

<table cellpadding="1" cellspacing="1" border="1">
    <tr>
        <th>Time</th>
        <th>My Time</th>
        <th>Diff in hours</th>
    </tr>
    <tr>
        <td><?php echo $datetime->format('Y-m-d H:i:s'); ?></td>
        <td><?php echo $newdate->format('Y-m-d H:i:s'); ?></td>
        <td><?php echo $diff->format('%h') ?></td>
    </tr>
</table>
07 Jul 2012

Twitter Bootstrap Live Tooltips

I had some problems with tooltips not loading on ajax loaded pages. The solution is pretty straightforward.

$('body').tooltip({
    selector: '[rel=tooltip]'
});
31 Mar 2012

Debian Squeeze Lamp Server

Here is a few commands to get a Squeeze development server up quick.

apt-get install mysql-server mysql-client

apt-get install apache2

apt-get install php5 libapache2-mod-php5

/etc/init.d/apache2 restart

apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

/etc/init.d/apache2 restart

apt-get install phpmyadmin

To search for PHP5 packages use apt-cache

apt-cache search php5

10 Mar 2012

SSH Key Authentication

To avoid entering a password on each SSH session you can generate ssh public keys and copy to the remote server. From the client machine use the following:

ssh-keygen -t dsa

Copy id_dsa.pub to remote server

scp ~/.ssh/id_dsa.pub remote_machine:/tmp

Login to the remote machine and append the keys to authorized_keys2 in your home folder

cat /tmp/id_dsa.pub >> ~/.ssh/authorized_keys2

07 Mar 2012

Xedebug with Cakephp and Netbeans

I’m using Debian and Xedebug is available via the package manager.

apt-get install php5-xdebug

Configure PHP to use xdebug.

vi /etc/php5/conf.d/xdebug.ini

; configuration for php xdebug module
zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so

xdebug.remote_enable = 1
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.remote_host = 10.11.228.223 ; [Remote address if debugging from a remote client]
xdebug.remote_port = 9000
xdebug.max_nesting_level = 500 ; [This may be needed for larger scripts]
xdebug.profiler_enable_trigger = 1 ; [Use trigger to enable with something like chrome/firefox extensions]
;xdebug.profiler_enable = 1

Configure netbeans under Tool > Options. Make sure PHP is configured with the proper port. You can remove the option to stop at first line. You must also make sure the project has the webroot defined under sources. Without this setting the debugger will not stop at the breakpoints.

02 Mar 2012

Enable mod_status for CAKEPHP

I’ve been trying to isolate a CPU issue for an app running on Apache2 written with Cakephp. Apache has a module to monitor the server status with mod_status. http://www.debian-administration.org/article/Monitoring_Apache_with_mod_status

For Apache 1.2 enable mod_info and restart apache.

sudo a2enmod info
sudo /etc/init.d/apache2 restart

You should also enabled ExtendedStatus.

sudo vi /etc/apache2/conf.d/extendedstatus

Add the following

ExtendedStatus On

Once the module is enabled you have to enable the settings in the appropriate vhost in /etc/apache2/sites-enabled

<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from localhost ip6-localhost
</Location>

The problem if you are using cakephp is that the .htaccess will try to route the request which will fail. You have to edit your .htaccess file to look like the following:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !=/server-status
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
19 Feb 2012

Find and Delete Files

If you need to find and delete files you can execute the following command.

find /path/to/search -name 'file-to-search-for' -exec rm -f \;

If you want to be prompted for each file you can use -i instead of -f

24 Dec 2011

Redirect root mail to external address in Debian

To redirect root mail to an external email just use /etc/aliases

# vi /etc/aliases

root: youremail@email.com

# newaliases
23 Aug 2011

Drop Foreign Key Constraint using INNODB

I recently needed to make some database modifications that required removing some existing foreign key constraints. The command looks like this.

ALTER table tablename DROP FOREIGN KEY foreign_key_name

MySQL was throwing errors.

mysql> ALTER TABLE reviews DROP FOREIGN KEY priority_id;
ERROR 1025 (HY000): Error on rename of ‘./dbname/reviews’ to ‘./dbname/#sql2-62f5-145b’ (errno: 152)

If you run

SHOW CREATE TABLE tablename

You’ll see the constraint names. You can then delete the foreign key based on the constraint.

ALTER TABLE tablename DROP FOREIGN KEY `reviews_ibfk_24`;

You can then drop the column as needed.

05 Aug 2011

MySQL Alter Table with InnoDB Foreign Key Constraint

In a recent project I needed to add a new table that would create a foreign key constraint on an existing table. I’m using the InnoDB storage engine. The existing table is employees and the new table is shifts. Shifts table is pretty simple.

CREATE TABLE shifts (
        id INT AUTO_INCREMENT NOT NULL,
        shift VARCHAR(15) NOT NULL,
        PRIMARY KEY(id)
) ENGINE=InnoDB;

The existing employees table needed a new column called “shift_id” that would reference the shifts table. The following code can be used to add the column and then add the foreign key constraint.

ALTER TABLE employees ADD shift_id INT AFTER group_id;
ALTER TABLE employees ADD CONSTRAINT FOREIGN KEY(shift_id) REFERENCES shifts(id) ON UPDATE CASCADE ON DELETE SET NULL;

The first alter statement worked fine, but the second kept throwing an error 150. You can view more details about the last error with the following:

SHOW INNODB STATUS;

I would see the following:

————————
LATEST FOREIGN KEY ERROR
————————
110805 8:49:16 Error in foreign key constraint of table backlogreviews/#sql-62f5_23:
FOREIGN KEY(shift_id) REFERENCES shifts(id) ON UPDATE CASCADE ON DELETE SET NULL:
Cannot resolve table name close to:
(id) ON UPDATE CASCADE ON DELETE SET NULL

This didn’t exactly help as I know the shifts table exists. I did some searching on google and found some hints, but these mostly indicated the column doesn’t match up correctly. Turns out I had a syntax error when creating the shifts table. The engine was INNOBD (notice the BD should read DB). Instead of throwing an error when creating the table it just used MyISAM. Since the storage engines didn’t match I was getting the error.