Question is following: I have an oracle trigger after row insert. From trigger, I want to call a php script and pass just inserted row fields as parameters.
Insert is coming from very old legacy application with a very little chance of looking at the source code. Rows are inserted frequently, could be batches of ~1000 rows at a time or could be one row in 30minutes, so checking this table every, let's say, 5 seconds is not an option.
So, Idea is to have oracle trigger, which would be triggered every time on insert and call my php script? Any ideas?
Thanks in advance...
When you say "php script" do you literally mean a command line script, or a chunk of php being run through apache/etc.
If its the former, then go with OMG Ponies. Otherwise I'd use UTL_HTTP to make the call to Apache/PHP. Actually, I'd probably consider going with this anyway ( updating your php/c# if needed ).
Just remember though.. Triggers are transactional... if you absolutely MUST call out from a trigger be aware that your trigger may run several times ( due to query restart ) and may rollback completely, resulting in your external (presumably non-transactional) php call now being invalid. If your php can't handle this then perhaps have your trigger creating a job or even a message into an AQ or something, this would also speed up handling, you probably don't really want your insert to be waiting on an external web call.
I want to call a php script when some row is inserted or updated in some table via oracle triggers . Currently I m using this but its not much of help.
CREATE OR REPLACE TRIGGER test_script
BEFORE INSERT OR UPDATE OR DELETE ON STATES
FOR EACH ROW
BEGIN
-- Flags are booleans and can be used in any branching construct.
CASE
WHEN INSERTING THEN
:'/! echo C:/wamp/bin/php/php5.3.5/php.exe C:/wamp/www/csv.php >> C:/wamp/www/log.txt'
WHEN UPDATING THEN
-- Include any code specific for when the trigger is fired from an UPDATE.
WHEN DELETING THEN
-- Include any code specific for when the trigger is fired from an DELETE.
END CASE;
END;
/
Related
I have a fairly large amount of data that I'm trying to insert into MySQL. It's a data dump from a provider that is about 47,500 records. Right now I'm simply testing the insert method through a PHP script just to get things dialed in.
What I'm seeing is that, first, the inserts will continue to happen long after the PHP script "finishes". So by the time I can see the browser no longer has an "X" to cancel the request and now has a "reload" (indicating the script is done from the browser perspective) I can see for a good 10+ minutes that inserts are still occurring. I assume this is MySQL caching the queries. Is there any way to keep the script "alive" until all queries have completed? I put a 15 minute timeout on my script.
Second, and more disturbing, is that I won't get every insert. Of the 47,500 records I'll get anywhere between 28,000 and 38,000 records but never more - and that number is random each time I run the script. Anything I can do about that?
Lastly, I have a couple simple echo statements at the end of my script for debugging, these never fire - leading me to believe that a time out might be happening (although I don't get any errors about time-outs or memory outages). I'm thinking this has something to do with the problem but am not sure.
I tried changing my table to an archive table but not only didn't that help but it also means I lose the ability to update the records in the table when I want to, I did it only as a test.
Right now the insert is in a simple loop, it loops each record in the JSON data that I get from the source and runs an insert statement, then on to the next iteration. Should I be trying to instead using the loop to build a massive insert and run a single insert statement at the end? My concern with this is that I fear I would go beyond my max_allowed_packet configuration that is hard coded by my hosting provider.
So I guess the real question is what is the best method to insert nearly 50,000 records into MySQL using PHP based on what I've explained here.
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
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.
I want a trigger to fire only when UPDATEs are user-initiated (not me running updates from the MySQL command line).
What's the 'industry standard' for achieving this? I have been trying unsuccessfully to detect a variable passed in with the query (i.e. #user_update=true), but without success. A colleague of mine suggested a way to do it would be to add a new column into the table containing the trigger: 'fire_trigger', for example. And fill this with 'true' when running the SQL from the code, and ensure that this is present in order for the trigger to fire.
Any help much appreciated!
Disable the trigger before doing command-line updates and re-enable it after.
But you're doing this wrong, conceptually.
Why shouldn't the trigger run when you use the command line? I Assume this is some sort of auditing trigger, that captures who updated a row. You at the command line are a "who" too.
Comment from OP:
Yes - good point! :) It is a "dd/mm/yyyy hh:mm - User A changed First name from "Jane" to "Joan"" type thing. I just don't want it to fill the history table during the test phase when I'm potentially updating rows 'manually'.
Then either disable the trigger, or just delete the added history rows after. Even better, do your testing on a separate database, and just don't worry about this at all.
Create 2 mysql users: one that is used by the application(so it will identify your users) and one that you will use when connecting through the mysql client.
In the update trigger, check for the user with CURRENT_USER() and take action only when it's the application user.
Hope it helps!