Catch mysql_query function in PHP to create custom trigger [duplicate] - php

This question already has answers here:
How to override built-in PHP function(s)?
(2 answers)
Closed 8 years ago.
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?

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.

Related

Where does codeigniter create and destroy mysql connection and is the db class singleton?

If we have multiple database groups in database.php:-
1) Do the connections of all of them are made even only one has to be used in a particular call. ie. if i have database groups a,b
And in my call i load model that is loading only group b.
2) If i have loaded two models in my controller and if both of them are loading same databases, would different connection will be made or same connection will be shared.
Ex:- controller mycont.php has following:-
$this->load->model('model1');
$this->load->model('model2');
If both model1.php and model2.php has following:-
$this->load->db('connection_name');
3) Where are the connections closed.
Ex:- If i have following code:-
$this->databaseFunc();//completes the database work nothing required after this
here a curl call is made which takes long time
So when does database connection is closed, after curl or it gets closed itself on over exceeding mysql_wait_time configuration at mysql server.
Hope the answer to this question will prove useful for understanding DB with codeigniter in a better way.
In CI, each library is a singleton. It is created on load->library and destroyed at the end of the request.
The database lib handle database connection, so the connection is closed when the library is destroyed. It has nothing to do with curl.
I've never tryed it but it should work like that.

Use MySQL and PostgreSQL in the same PHP script

I'm trying to do some migrations from an old site to a new site. The old site uses MySQL and the new site uses PostgreSQL. My problem is I wrote a migration script in PHP that queries info from the old DB so that I can insert them into the new DB within that same script. The reason I need the script is I have to call other functions that do things and manipulate the data since the table columns aren't a one for one match so I can't just do a backup and restore type situation. I have a class for both DB's that I use.
The mysql queries work but postgres' don't. They get error messages saying pg_query(): 19 is not a valid PostgreSQL link resource in xxx
So is it possible to run them both in the same script? If I call the two scripts separately it works ok but I can't get the data from the old server to the new one.
I've looked everywhere and don't see many questions needing to use both DB's in one file.
Any help would be cool.
You are using the same variable for both resources and passing the mysql resource to the postgresql function

How can I solve this Solr/MySQL race condition?

I'm experiencing a very strange problem whereby my Solr index is not able to see a change just written to a MySQL database on another connection.
Here is the chain of events:
The user initiates an action on the website that causes a row to be added to a table in MySQL.
The row is added via mysql_query() (no transactions). If I query the database again from the same connection I can naturally see the change I just made.*
A call is immediately sent to a Solr instance via curl to tell it to do a partial update of its index using the Data Import Handler.
Solr connects to the MySQL database via a separate JDBC connection (same credentials and everything) and executes a query for all records updated since its last update.
At this point, however, the results returned to Solr do not include the last-added row, unless I insert a sleep() call immediately after making the change to the database and before sending the message to Solr.
*Note that if I actually do query the database at this point though, this takes enough time for the change to actually be picked up by Solr. The same occurs if I simply sleep(1) (for one second).
What I'm looking for is some reliable solution that can allow me to make sure the change will be seen by Solr before sending it the refresh message. According to all documentation I've found, however, the call to mysql_query() should already be atomic and synchronous and should not return control to PHP until the database has been updated. Therefore there doesn't appear to be any function I can call to force this.
Does anyone have any advice/ideas? I'm banging my head over this one.
Check what the auto-commit is set to when inserting the record. Chances are the record just inserted is in the same database session and thus is seen (but isn't committed). After this, some event causes the commit to occur and hence another thread/session can then "see" the record. Also check the transaction isolation level settings.
I typically do not use the Data Import handler and would have the update in the website trigger a mechanism (either internal or external) to update the record into Solr using the appropriate Solr Client for the programming language being used. I have personally not had a lot of luck with the Data Import Handler in the past and as a result have preferred to use custom code for synchronizing Solr with the corresponding data storage platform.

Force MySQL to write back

I have an issue where an instance of Solr is querying my MySQL database to refresh its index immediately after an update is made to that database, but the Solr query is not seeing the change made immediately prior.
I imagine the problem has to be something like Solr is using a different database connection, and somehow the change is not being "committed" (I'm not using transactions, just a call to mysql_query) before the other connection can see it. If I throw a sufficiently long sleep() call in there, it works most of the time, but obviously this is not acceptable.
Is there a PHP or MySQL function that I can call to force a write/update/flush of the database before continuing?
You might make Solr use SET TRANSACTION ISOLATION LEVEL = READ-COMMITTED to get more prompt view of updated data.
You should be able to do this with the transactionIsolation property of the JDBC URL.

Live update notification on database changes MYSQL PHP

I was wondering how to trigger a notification if a new record is inserted into a database, using PHP and MySQL.
You can create a trigger than runs when an update happens. It's possible to run/notify an external process using a UDF (user defined function). There aren't any builtin methods of doing so, so it's a case of loading a UDF plugin that'll do it for you.
Google for 'mysql udf sys_exec' or 'mysql udf ipc'.
The simplest thing is probably to poll the DB every few seconds and see if new records have been inserted. Due to query caching in the DB this shouldn't effect DB performance substantially.
MySQL does now have triggers and stored procedures, but I don't believe they have any way of notifying an external process, so as far as I know it's not possible. You'd have to poll the database every second or so to look for new records.
Even if it were, this assumes that your PHP process is long-lived, such that it can afford to hang around for a record to appear. Given that most PHP is used for web sites where the code runs and then exits as quickly as possible it's unclear whether that's compatible with what you have.
If all your database changes are made by PHP I would create a wrapper function for mysql_query and if the query type was INSERT, REPLACE, UPDATE or DELETE I would call a function to send the respective email.
EDIT: I forgot to mention but you could also do something like the following:
if (mysql_affected_rows($this->connection) > 0)
{
// mail(...)
}
One day I ask in MySQL forum if event like in Firebird or Interbase exist in MySQL and I see that someone answer Yes (I'm really not sure)
check this : http://forums.mysql.com/read.php?84,3629,175177#msg-175177
This can be done relatively easily using stored procedures and triggers. I have created a 'Live View' screen which has a scrolling display which is updated with new events from my events table. It can be a bit fiddly but once its running its quick.

Categories