可以在 MySQL 上执行 PHP 插入吗?
我实际上想在记录添加到数据库时通过邮件或以某种方式通知用户。有可能吗?就像ON INSERT
触发器一样,我执行一些PHP?
I actually want to mail or somehow notify a user when a record has been added to the database. Is it possble? Like ON INSERT
trigger, I execute some PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法使用 mysql 事件执行 PHP 脚本。
你必须在 PHP 端执行此操作。
检查插入命令是否执行成功并发送邮件。
You can not execute PHP script with mysql events.
You have to do this in PHP end.
Check if the insert command is executed successfully and send mail.
只需检查插入是否成功,然后发送邮件给客户即可。
Just check if the insert is succesfull and then send mail to the customer.
你不能在 mysql 查询中执行 php,但你可以检查查询是否成功,然后向某人发送邮件,
mysql_query()
将返回一个boolean
(true< /code> 或
false
)。如果您使用 php,让我们以正确的方式使用它;)you cant execute php in a mysql query but you can check if the query was made successfully and then mail someone,
mysql_query()
will return aboolean
(true
orfalse
). if you are using php, let's use it the right way ;)通过安装 MySQL sys_exec UDF 可以从 MySQL 触发器内执行外部程序
,但是,它这样做似乎是有开销的。当 PHP 执行插入并且此时您知道您输入了数据时,为什么要让 MySQL 触发通知?
It is possible to execute external programs from within MySQL triggers by installing MySQL sys_exec UDF
However, it seems that it's an overhead to do so. Why would you make MySQL trigger the notification when it's PHP that does the insert and at that point you know you entered the data?
生成有关数据库插入/修改/删除的通知(电子邮件等)的最快、最简单的方法是通过在数据库中执行更改的脚本执行此操作。
如果做不到这一点,如果您不自己执行数据库操作(出于某种原因),或者您希望能够执行定时摘要(每小时、每天、每周),则需要使用类似 Cron Job 的工具来轮询数据库并检查更改。
The quickest and easiest way of generating notifications (emails, etc.) on database insertions/modifications/deletions is by doing so from the script which is performing the change in the database.
Failing that, in the event that you are not performing the database manipulations yourself (for some reason), or you want to be able to perform timed summaries (hourly, daily, weekly), you would need to use something like a Cron Job to poll the database and check for changes.