关于数据库更改的实时更新通知 MYSQL PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/743346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:43:01  来源:igfitidea点击:

Live update notification on database changes MYSQL PHP

phpmysql

提问by mohamadfikri

I was wondering how to trigger a notification if a new record is inserted into a database, using PHP and MySQL.

我想知道如果使用 PHP 和 MySQL 将新记录插入到数据库中,如何触发通知。

回答by Assaf Lavie

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.

最简单的事情可能是每隔几秒钟轮询一次数据库,看看是否插入了新记录。由于数据库中的查询缓存,这不应显着影响数据库性能。

回答by Alnitak

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.

MySQL现在确实有触发器和存储过程,但我不相信他们有任何通知外部进程的方法,所以据我所知这是不可能的。您必须每隔一秒左右轮询一次数据库以查找新记录。

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.

即使是这样,这也假设您的 PHP 进程是长期存在的,因此它可以承受等待记录出现的时间。鉴于大多数 PHP 用于代码运行然后尽快退出的网站,目前尚不清楚这是否与您所拥有的兼容。

回答by steveayre

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.

您可以创建一个触发器而不是在发生更新时运行。可以使用 UDF(用户定义函数)运行/通知外部进程。没有任何内置方法可以这样做,所以这是加载一个可以为您完成的 UDF 插件的情况。

Google for 'mysql udf sys_exec' or 'mysql udf ipc'.

谷歌搜索“mysql udf sys_exec”或“mysql udf ipc”。

回答by Alix Axel

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.

如果您的所有数据库更改都是由 PHP 进行的,我将为 mysql_query 创建一个包装函数,如果查询类型是 INSERT、REPLACE、UPDATE 或 DELETE,我将调用一个函数来发送相应的电子邮件。

EDIT: I forgot to mention but you could also do something like the following:

编辑:我忘了提及,但您也可以执行以下操作:

if (mysql_affected_rows($this->connection) > 0)
{
    // mail(...)
}

回答by Hugues Van Landeghem

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)

有一天我在 MySQL 论坛上问 MySQL 中是否存在 Firebird 或 Interbase 之类的事件,我看到有人回答是(我真的不确定)

check this : http://forums.mysql.com/read.php?84,3629,175177#msg-175177

检查这个:http: //forums.mysql.com/read.php?84,3629,175177#msg-175177

回答by Hugues Van Landeghem

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.

这可以使用存储过程和触发器相对容易地完成。我创建了一个“实时视图”屏幕,它有一个滚动显示,它会更新我的事件表中的新事件。它可能有点繁琐,但一旦它运行得很快。