Category: programming

  • Backgrounded PHP jobs in ‘STOPPED’ state

    This could be specific to the PHP package from Ubuntu. Please consider the following a disclaimer. $ php -v PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May 2 2011 23:00:17) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans $ cat /etc/lsb-release…

  • Checking for the existence of domains from commandline in Bash

    while read line; do wget –quiet –spider –timeout=10 $line; if [ $? -ne 0 ]; then echo $line; fi; done < "/tmp/domains.txt" /tmp/domains.txt would hold the list of domain names separated by newline. Note this is just a quick hack and not entirely reliable.

  • myisamcheck running out of tmp space

    Recently I was running myisamcheck on an entire database housed inside a vserver. And, by default vserver mounts a 16MB ramfs on /tmp. This obviously is not an ideal solution when you are running a DB server that has a few tables that are around 20GB. So I had to specify a different directory for…

  • Getting rid of MySQL Warning: Truncated incorrect DOUBLE value

    If you come across a cryptic warning like this: | Warning | 1292 | Truncated incorrect DOUBLE value: ‘xxxxxxx’ on running a MySQL query, it could be caused by using a numeric value against a CHAR/VARCHAR column. —assuming `name` is a CHAR/VARCHAR column —the following query might cause warnings SELECT * FROM `t1` where `name`…

  • PHP as a replacement for sed/awk

    I had gotten sick of not being able to do what I wanted to do with sed/awk – partly because I am not too familiar with those – and, was investigating replacements for those. Many people seem to be using perl and since I wasn’t too familiar with Perl as well, I started thinking about…

  • Regular Expression to check for comma separated list of Integers

    //get rid of all kinds of spaces $IDs = preg_replace(‘/s+/’, ”, $inputIDs); if(!preg_match(”/^\d+$|^(\d+,)+\d+$/”, $IDs)) { echo “Invalid Input”; } You could use the validated IDs above in a SQL like the this: SELECT * FROM t1 WHERE id IN($IDs) Disclaimer: I am not going to be responsible for any kind of SQL injection resulting from…

  • Bulk Insert while using PDO

    If you are looking to Bulk Insert while using PDO and want to use bound parameters (to prevent SQL injection), please have a look at this solution here: http://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo Quite simple and does the job.

  • PHP, MongoDB, findAndModify

    findAndModify is not supported by the current version of the PHP MongoDB driver, so I had to use the following code $feed = $db->command(array(‘findAndModify’ => ‘feedList’, ‘query’ => array(‘$where’ => new MongoCode(‘ function(){ return ((this.inProgress == false) || (currentTime >= (this.lastUpdated + this.updateFrequency)))); }’, array(‘currentTime’ => $start))), ‘update’ => array(‘$set’ => array(‘inProgress’ => true)))); Where:…

  • Munin: Could not open required defaults file: /root/.my.cnf

    For some reason I kept getting this error (even when the file was readable by all) in one of the munin (mysql slave) plugins that I had written. So this was the solution that worked for me; under the [mysql*] section in /etc/munin/plugin-conf.d/munin-node I added this: env.mysqlopts -u{user} -p{password} and in my plugin I did…

  • MySQL KEY, PRIMARY KEY and NOT NULL.

    Have a look at this sql statement: mysql> CREATE TABLE `t1` (`id` INT NULL AUTO_INCREMENT, KEY(`id`)); Query OK, 0 rows affected (0.92 sec) mysql> SHOW CREATE TABLE `t1`; +——-+————————————————————————————————————————-+ | Table | Create Table | +——-+————————————————————————————————————————-+ | t1 | CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |…