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.
Related
How is it possible to store data in the database for a limited time (like for example 1 hour)?
A user searches a certain thing, the logic is executed server-side and the search result is loaded. As the result is loaded, I want to store it in MySQL and keep it only x hours. After x hours this data should be deleted from the database.
How is it possible to do this in Laravel or in PHP (It does not matter for me. I just mentioned about Laravel as it may have some libraries for this)? Is it about writing logic in SQL or it's a PHP task?
How to do this? Any suggestions/links/solutions?
Thank you very much!
Is it about writing logic in SQL or it's a PHP task?
Doing this with php would be a hassle because you would always need to send requests to check if a record is expired and then send another to delete. This can be achieved using events in mysql. I came across this blog post which might be helpful.
Also dont forget to check the mysql documentation page
Tag records with an expiration timestamp, and have a cron job run every minute (or whatever frequency you prefer) to delete expired records.
The best solution for this task would be to use the Redis database. There are 2 drivers for PHP: pRedis and phpRedis.
The pRedis is a implementation written in PHP which works relatively very slow for a big number of users.
The phpRedis driver is written in C, and it works much faster.
Use the $redis->psetex($key, $destroy_time, $value) function to create self-destroying records. Hope it helps anyone in future
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.
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.
We have a web application running on LAMP stack, the application depends on various services. These services gets data from cache (memcached) which is being refreshed using cron (from MySQL). Cron processes are running every 5 mins.
In this approach we can not serve data which is updated recently as cache is getting refreshed every 5 mins.
Is there any mechanism exists which can trigger cache refresh as soon as data gets updated in MySQL?
I don't know if this is the best solution, but what you can do is create MySQL trigger which gets executed on insert/update/delete.
Inside that MySQL trigger execute a UDF. From that UDF you can execute any PHP script using sys_exec().
Read about Triggers
Read about UDF
Read about using sys_exec() and more
My solution for this problem was to have the MySQL query in a function with the memcached query, for example (in Python, because I don't know PHP, you can look at it an change it to PHP):
def insert(user,value):
#execute on memcached first
key="user:"+user
memcClient.set(key,value)
#then execute it on MySQL
cursor.execute("INSERT INTO tables VALUES (%s,%s)",(user,value))
db.commit()
And you will do the same for delete and update, sorry I couldn't do it in PHP, I'm just a teen coder, good luck.
What about identifying the mysql updation at php level only & refresh the memcache accordingly ?
<?php
if($update_db){ // if we need to update the db then update db & memcacache together !
// Code to Update the database ....
// Code to Reset the memcached keyvalue pair.
}
?>
In the approach suggested by Manu, we are using expensive db trigger which is actually not needed to achieve the cache update.
I am using MYSQL under ubuntu. I want to run a certain program automatically when inserting or updating row in a certain table. The program is actually sending request to a php on the same server. The php script is implemented and it notifies all clients that "data is updated, please get it". How can I do it?
Thank You
best solution is create cron job and use system command in cron file.
If you have control of the script, the easiest way would be to create your own query() method that wraps around whatever SQL query call you need to make. You can put something in there to see if there's an UPDATE/DELETE/INSERT and if so fire off your "data updated" notice.
Probably cannot be done without major security issues on mysql server.
you could to this from php. either execute the program when you send the query to mysql or create a cronjob