Saturday, May 29, 2010

Sending PHP otput to a file

function ob_callback($buffer) {
global $ob_file;
fwrite($ob_file, $buffer);
}

$ob_file = fopen('test.txt','w');
ob_start("ob_callback");

/* Here goes content. */

ob_end_flush();

Sunday, May 16, 2010

Thursday, May 13, 2010

Copying selected record from one MySQL table to another

In the case of existing table with the same structure:
INSERT table2 SELECT * FROM table1 WHERE [conditions]

Alternative syntax for a table with different structure:
INSERT table2 (columnA, columnB) SELECT column1, column2 FROM table1 WHERE [conditions]

Saturday, May 8, 2010

print_r() output to a file

ob_start();
print_r($array);
$output = ob_get_clean();
file_put_contents("log.txt", $output);

Saturday, May 1, 2010

Converting a date from MySQL to human readable format

date("M j, Y", strtotime($MySQLDate))

MySQL random integer in the range

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)).