0

I have huge project in PHP, which is using mysql_connect function in lots of files. My goal is to detect how many times mysql_connect was triggered from PHP. (from this project).

Idea is to not modify existing project files and create some parallel function (trigger) which will run only when this project will call mysql_connect in PHP.

Is there any way to configure PHP to create some trigger on mysql_connect? I mean, when PHP will call mysql_connect, this trigger must detect this and parallely run some another script, which will do counting or other stuff.

Any ideas?

zur4ik
  • 6,072
  • 9
  • 52
  • 78
  • 2
    http://www.php.net/manual/en/function.override-function.php Override it and log that stuff! (Check out the first user contributed example) – AmazingDreams Dec 18 '13 at 16:07
  • Or try using namespaces for this job > http://stackoverflow.com/questions/6932177/override-a-default-php-function-eval – Glavić Dec 18 '13 at 16:22
  • You might be able to use the logging capabilities of the Suhosin patch for this. Maybe. – Spudley Dec 18 '13 at 16:36

1 Answers1

0

There is no trigger event that is fired on new connections (at least none listed here: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html)

But you can ask MySQL directly how many connections have been attempted (successful or not)

show status like 'Connections'

(See: http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html#statvar_Connections for more status information)

Maybe it also helps you to know that each connection gets a unique connection id which you can retrieve via

SELECT connection_id()

So if you have a table that is queried by each script your trigger could react to actions on that table, get the connection id of the active connection and write it somewhere if it has not been logged before.

Beware that the enumeration of connections starts at the beginning if the MySQL server is restarted. This also applies to MySQLs internal connection count which you can retrieve with show status.

But as already mentioned, I would also strongly consider to solve this problem on the side of PHP.

Michael Helwig
  • 530
  • 4
  • 12
  • I don't think the question is asking about mySQL triggers. I think he's asking about triggering a script when PHP does something (ie connect to the DB). The choice of the the word 'trigger' in the question is unfortunate given the existence of a mySQL feature with the same name, but reading the question it's fairly clear that isn't what he wants. – Spudley Dec 18 '13 at 16:35
  • Yeah, maybe you are right and I was mislead by the wrong terminology. – Michael Helwig Dec 18 '13 at 16:38