mysql

Exception handling in PHP

So today I ripped my hair (the few that's left) from my head, because my code didn't work as supposed, but no error was shown by my try/catch block, when accessing my database.

So this was the code I worked with:

$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
$dbo = new PDO($dsn, DB_USER, DB_PASS);

and I had to add this, to actually trigger some exceptions:

$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Running mean in MySQL

So I wanted a graph, which showed the actual temperature for the last 48 hours, and a running mean to go with it:

SELECT convert_tz(date,'gmt','cet') AS date, temperature, 
(CASE WHEN (@n := @n + 1) IS NULL THEN NULL 
    WHEN (@cumval := @cumval + temperature) IS NULL THEN NULL
    ELSE (@cumval / @n) END) AS runningmean 
FROM weather 
CROSS JOIN (SELECT @n := 0, @cumval := 0) AS vars 
WHERE convert_tz(date,'gmt','cet') >= now() - interval 48 hour 
ORDER BY 1

Speaks for itself

Subscribe to RSS - mysql