Wednesday, February 10, 2010

Bug PDO MySQL 5.0.67 (Linux only)

My configuration :

  • - CentOS 5 (same problem with RedHat)
  • - MySQL 5.0.67
  • - PHP 5.1.6
  • - PDO lib 5.0.67

I may encounter such fatal errors :

Fatal error: Call to a member function fetch() on a non-object...

It looks like the PDO Statement has been killed or doesn't respond... For the same processing, running on Windows, with MySQL 5.1.30 and PDO lib 5.0.51a, everything works...
When I execute code on Windows server, targetting database to my Linux server, it works well. So I can rely on MySQL server...
Using a mysql_connect() command also works, so I guess the problem comes from PDO libraries...

A very few people mentionned this issues on technical forums that's why I open this post to anyone who's been annoyed with it...



Updated on 2010-03-01

Here is a solution :

$queryTypRef = myQuery;
$stmtTypeRef = $this->_dbOps->query($queryTypRef);
$resTypeRef = $stmtTypeRef->fetch();

$stmtTypeRef->closeCursor();
$stmtTypeRef = null;

Monday, February 8, 2010

Performance measures

How to introduce performance measures in your logging system ?


An easy way to measure response time is to use microtime() function.

PHP official documentation :
microtime() returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call.

In order to measure a response time of a request, you have to identify its entry point. Then put the Start top as the first action. Finally, detect the request response point and put the Final top just above...

//Launch timer
$time0 = microtime(TRUE);
$time00 = $time0;
echo 'Call: '.$time00.' - TIMER Request toto - Start : '.$time0.'<br/>';

//Response initialization
$response = 'Here is the final response to client !';

//some actions
usleep(100000);

//some intermediate measure
$timeX = microtime(TRUE);
$timeXDuration = $timeX - $time0;
echo 'Call: '.$time00.' - TIMER Request toto - Intermediate X : '.$timeX.'<br/>';
echo 'Call: '.$time00.' - TIMER Request toto - Intermediate X Duration : '.$timeXDuration.'<br/>';

//Stop timer
$timeResponse = microtime(TRUE);
$timeDuration = $timeResponse - $time0;
echo 'Call: '.$time00.' - TIMER Request toto - Response time : '.$timeResponse.'<br/>';
echo 'Call: '.$time00.' - TIMER Request toto - Duration : '.$timeDuration.'<br/>';

return $response;

This code displays :

Call: 1265645157.8705 - TIMER Request toto - Start : 1265645157.8705
Call: 1265645157.8705 - TIMER Request toto - Intermediate X : 1265645157.9706
Call: 1265645157.8705 - TIMER Request toto - Intermediate X Duration : 0.10011100769043
Call: 1265645157.8705 - TIMER Request toto - Response time : 1265645157.9707
Call: 1265645157.8705 - TIMER Request toto - Duration : 0.10015106201172

Note that $time00 is used to identify the call. It could be replaced by a random string, and you can pass it as a parameter to sub-functions to measure sub-treatments...