Thursday, 17 December 2009

Enable php5 on 1and1

Arrrggg - made a website in php5 and then get loads of errors when uploading to 1and1? Luckily you can change the default setting by adding the following line to a .htaccess file;

AddType x-mapp-php5 .php

Hope this helps - I found this information on the following link;


Sunday, 4 October 2009

PHP convert minutes to hours

PHP convert minutes to hours

Just a little function i made;

function minutes_to_hours($inmin){
$lh = floor($inmin/60);
$lm = $inmin - ($lh*60);
if ($lh > 0) return $lh.'h '.$lm.'m';
return '0h '.$lm.'m';

So calling minutes_to_hours(125) = 2h 5m

Tuesday, 22 September 2009

Retrieve Mysql Lost Data

Right, well have you ever been in a position where you have lost information on websites through incorrect sql? Well, chances are that not everything is lost, especially with mysql.

Mysql records all sql statements run on mysql a compressed binary log. Everytime mysql restarts, a new log is created. In fedora, these logs are located in /var/lib/mysql/. For example, you will see files named mysql-bin.000001. However, to make use of them, first you must extract them into a readbale forma using the mysqlbinlog tool.

mysqlbinlog takes a binary log file and then does something with it. For example, to view the file, simply use the following example;

mysqlbinlog mysql-bin.000001

To write the log to a singel file, use the following command;

mysqlbinlog mysql-bin.000001 > all_sql_statements.sql

Then you can simply read the fil and take the statements you need from it.

Thursday, 16 July 2009

PHP Session Test

Recently, i've had problems setting a clients site live on a 3rd party providers hosting. The problem is the site wouldn't allow sessions to be written so login scripts and catcha codes did not work as intended. The main problem is that technical support always blame programmers code before actually investigating the problem (sigh).

Therefore here is a simple php script to prove to the technical support that sessions are not working as they should;

if($_GET["test"] == '1'){
if($_SESSION['atest'] == 'yes'){
echo('Your hosting supports sessions');
} else echo('Your hosting does not support sessions');
} else {
$_SESSION['atest'] = 'yes';
echo '
<script type="text/javascript">
function delayer(){
window.location = "'.$_SERVER['PHP_SELF'].'?test=1";
<body onLoad="setTimeout(\'delayer()\', 5000)">
About to test sessions.
<br /><br />
please wait...

The script performs a 301 redirect to test if sessions are working correctly when a page is refreshed.

Sunday, 25 January 2009

Mysql Remove Line Breaks

I've recently had line breaks added to some data i've imported. Here's how you remove line breaks at the beginning and end of a field in mysql;

update temp_table set fieldname=trim(both char(13) from fieldname)

The above is only if you are in windows. I believe you can also use the folowing for linux;

update temp_table set fieldname=trim(both '\n' from fieldname)

You should also be able to use reaplce usinng the char method, e.g.

update temp_table set fieldname=replace(fieldname, char(13), '-')

I've not tested it though - I hope this helps!

Friday, 16 January 2009

MySQL Monthly Report

Here is is brief howto on generating a Monthly MySQL report from an orders table.

This example assumes you have a table called orders a stored date field called added and a price field for each order - in this example the price is stored as a varchar so it is casted using a (0+price).

The fist column returned is the current month, the second is the total price for that month. Here is the MySQL SQL Monthly Report Query;

SELECT TIMESTAMPDIFF(MONTH, STR_TO_DATE(concat(month(added),'/1/',year(added)), '%m/%d/%Y'), STR_TO_DATE(concat(month(now()),'/',DAYOFMONTH(LAST_DAY(now())),'/',year(now())), '%m/%d/%Y')) as tmonth,
sum(0+total_price) as lprice
from orders
where valid=1 and total_price > 0
group by tmonth

There probably are quicker ways of doing this but it seems like the best solution for reporting on a single orders table. This example basically makes the date as a string using CONCAT and then compare it.

I hope this helps.