Automatic Backup Of Any PostgreSQL Table Using One Trigger Function

May 7th, 2013

If you manage sensitive information which is supplemented with inserts, updates and deletes on a regular basis, you know that all it takes is one bad query or one rogue process to completely destroy your data. Not even replication can save you from “DELETE FROM my_table; COMMIT;”.

With this exact problem in mind, we set out on a mission to deliver a solution which allows one to selectively restore data from any table across any schema at any time, which may or may not be replicated or have a hard backup of.
Read the rest of this entry »

E-Mail Attachments With PHP’s mail() Function

May 2nd, 2013

Below is a full working example of how to include one or more attachments to an outbound e-mail utilizing PHP’s mail() function.
Read the rest of this entry »

How To: Return Updated Row ID in PostgreSQL, SQL Server and MySQL

February 19th, 2013

PostgreSQL

PostgreSQL’s `RETURNING` clause is an officially supported implementation. A single column, multiple columns, or the entire row may be returned.

Return the `id` column

UPDATE my_table SET my_col = 'My Value' WHERE id = 123 RETURNING id

Return `id` and `my_col` columns

UPDATE my_table SET my_col = 'My Value' WHERE id = 123 RETURNING id, my_col

Return all columns

UPDATE my_table SET my_col = 'My Value' WHERE id = 123 RETURNING *

Microsoft SQL Server

MSSQL Server’s `OUTPUT` is an officially supported implementation. A single column, multiple columns, or the entire row may be returned.

Return the `id` column

UPDATE TOP (1) my_table SET my_col = 'My Value' OUTPUT Inserted.id WHERE id = 123

Return `id` and `my_col` columns

UPDATE TOP (1) my_table SET my_col = 'My Value' OUTPUT Inserted.id, Inserted.my_col WHERE id = 123

Return all columns

UPDATE TOP (1) my_table SET my_col = 'My Value' OUTPUT Inserted.* WHERE id = 123

MySQL

MySQL does not support returning when performing an UPDATE. To get around this, you can make use of the LAST_INSERT_ID() function. Unfortunately, this limits us to only returning a single column, and that column must be a type of integer (tinyint, smallint, int or bigint, signed or unsigned).

For this to work, either your driver must support multiple queries such as PHP’s mysqli::multi_query(), or you must perform two queries to retrieve the updated id. One for updating, one for retrieving the updated value.

Update and return the id in a single query:

UPDATE  my_table
SET
	id = LAST_INSERT_ID(id),
	my_col = 'My Value'
WHERE
	id = 123;
SELECT LAST_INSERT_ID();

Update in one query, returning `id` in a subsequent query:

UPDATE  my_table
SET
	id = LAST_INSERT_ID(id),
	my_col = 'My Value'
WHERE
	id = 123
SELECT LAST_INSERT_ID()

If you’re using the mysql_query() function to perform updates in PHP, instead of performing the subsequent query, you can simply make use of mysql_insert_id(). Example:

<?php
mysql_query("UPDATE my_table SET id = LAST_INSERT_ID(id), my_col = 'My Value' WHERE id = 123");
$id = mysql_insert_id();
?>

Outstanding Honors Award: Namecheap

February 4th, 2013

When it comes to online-based businesses and services it’s hard enough to know whether or not a web site is even legitimate, let alone get a clue into what their stance on various political topics may be. Out of the hundreds of millions of registered domains and tens of thousands of businesses a few keystrokes away, there is one company which made it’s way to the forefront of my attention and in doing so has forever changed the way I look at the businesses and organizations I support. Read the rest of this entry »

Installing the hstore Module in PostgreSQL 8.4 or 9.1

January 5th, 2013

If you’re looking for a quick reference guide to installing the hstore contrib module (or any other contrib module for that matter,) here you are. Provided are instructions for both PostgreSQL 8.4 as well as 9.1. Read the rest of this entry »