简体   繁体   English

侦听 mySQL 数据库中的更改(使用 PHP)

[英]Listening for changes in a mySQL database (with PHP)

Lets say I have a system, in which the operator opens a slot (which creates a new row in a mySQL DB) and then a QR code appears on screen.假设我有一个系统,其中操作员打开一个插槽(在 mySQL DB 中创建一个新行),然后屏幕上出现一个二维码。 The user scans it with his iPhone, then presses 'approve', which changes one of the columns in the new row to true .用户用他的 iPhone 扫描它,然后按“批准”,这会将新行中的一列更改为true

How can I make it so when it does change to true , a message will pop up on the operator screen?当它更改为true时,我怎样才能做到这一点,操作员屏幕上会弹出一条消息?

I thought of two ways that might be possibly done:我想到了两种可能的方法:

  1. a change in the row can trigger something that will push a message to the operator's machine.行中的更改可以触发将消息推送到操作员机器的某些内容。
  2. if the operator can go into a page, that will only load up when the row = true , but I can't figure out how to do that.如果操作员可以进入一个页面,那只会在 row = true时加载,但我不知道如何做到这一点。

If you know how to do some of the stuff I listed above, or have a different idea, please share it, it will be highly appreciated :D如果你知道如何做我上面列出的一些东西,或者有不同的想法,请分享,我们将不胜感激:D

I would use an AJAX call.我会使用 AJAX 调用。 Make a php page that checks the database and returns a very simple yes/no response that can be read by javascript.制作一个 php 页面来检查数据库并返回一个非常简单的是/否响应,可以被 javascript 读取。 Then, use ajax to query that on an interval (such as every 5 seconds) until the page returns a yes.然后,使用ajax每隔一段时间(例如每5秒)查询一次,直到页面返回yes。 After that, you can easily alert the user (the javascript alert() function would work, or you might want to code up something fancier).之后,您可以轻松地提醒用户(javascript alert() 函数可以工作,或者您可能想要编写更高级的代码)。

Easy solution: Just create a flag in your database boolHasBeenShowed .简单的解决方案:只需在您的数据库boolHasBeenShowed创建一个标志。 When someone presses approve you set the boolHasBeenShowed to true.当有人按下批准时,您将boolHasBeenShowed设置为 true。

When the operator opens the admin page, check the database for all the records WHERE boolHasBeenShowed = TRUE .当操作员打开管理页面时,检查数据库中的所有记录WHERE boolHasBeenShowed = TRUE As soon as they have been showed, UPDATE the boolHasBeenShowed = false .一旦它们被显示, UPDATE boolHasBeenShowed = false That way you will only see the newly approved records.这样,您将只能看到新批准的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM