How to track Update in database table using PHP? - php

I want to ask is there a way to track database table using php mysql.
I want to do something like i have a table called post. Now when user post some data other user need to view this data. That is latest one need to be view to user on the top. We can do this by refreshing div after every few sec or using ajax. But can we use Trigger. As we know it automatically fires when something is executed. Hence i want to know can we use trigger in PHP code to automatically detect changes in table. And when a new post is available it needs to return the data from database. Please give me a brief description about this. Thank you in advance.

The trigger is executed on Mysql Server, not on the PHP one (even if those are both on the same machine).
So, I would say this is not quite possible -- at least not simply.
Still, considering this entry from the MySQL FAQ on Triggers :
23.5.11: Can triggers call an external application through a UDF?
Yes. For example, a trigger could invoke the sys_exec() UDF available at MySQL Forge here: http://forge.mysql.com/projects/project.php?id=211
So, there might be a waty, actually, via an UDF function that would launch the php executable/script ; not that easy, but seems possible ;-)
Read more about it in:
https://stackoverflow.com/a/1467387/3653989

SQL trigger is a database object executed server-side.
You want a front-end technique to refresh your data without refreshing the whole page.
You can refresh your page using:
<meta http-equiv="refresh" content="5">
With PHP, you can refresh the page using:
header("refresh: 3;");
but no-one would suggest you to use such a method, because your need is refreshing the page, only after a change in your database, and not continuously.
So, if you already use PHP, you need Javascript Push technology:
Push, or server push, describes a style of Internet-based communication where the request for a given transaction is initiated by the publisher or central server. (wikipedia)
JavaScript polling, long-polling, real-time techniques, and javascript frameworks such as jquery, node.js, socket.io include a lot of practices that give you this possibility.

Related

reflect the list without refreshing the page in php

I am trying to show the list of online users in my application. Let me explain my requirement.
I have a Mysql DB table where list of username and their status mode(either 1 or 0) are storing. I have two php pages. One is for listing down all user's name and the status mode. Second page is for editing the mode of users from 1 to 0 and vice versa.
Now I open these two pages from different system. If I change the status of one user(edit page) from one system then automatically it will reflect to the another system, where the listing page is opened, with the updated record and obviously this should happen before refresh the listing page. The same like gtalk chat users.
I am not asking the code, but please help me how to proceed to resolve the issue. Obviously, cronjob is one of the solutions, please provide another solution.
Thanks in advance.
Well cronjobs are in fact not the thing you need.
With cronjobs you can schedule a task. What you want is client side refresh when new info is found. While cronjobs are server side and always on an interval.
What you need is polling or commet
The first, polling, you use your client side to execute a script every x seconds and look if there is new info (waste of resources in my opinion).
Commet, is now a days a better solution. But often hard to implement. I used pusher for this type of stuff. You can push messages to (all) clients connected and say there is new info. And then they will update or with the message comes also the new info
To achieve something like this, you should use JavaScript and Ajax in the clientside.
Give the XMLHttpRequest a try. To make it easier you could use something like jQuery.
On the serverside you could use json to transmit the data.
Read the data from the table and put it into an array, let's call it users, the keys are the names and the values are their mode(1 or 0).
Then use json_encode(ARRAY):
//Echo the results in json format
echo(json_encode($users));
Let's say, the users 'Frank', 'Susan' and 'George' are online and 'Isabell' and 'John' are offline. Then the script would result in an output similar to this:
{"Frank":1,"Susan":1,"George":1,"Isabell":0,"John":0}
Of course you need to put this and the loading into another php-script, maybe refresh.php.
And, to read the data from the script, add some JavaScript to your view page.
Use the XMLHttpRequest to request data from the script you just added.
Or, if you use jQuery, you can simply use $.getJSON("NameOfTheScriptYouJustWrote") which returns an already parsed object.
Then use the returned data to update the list of users. And refresh it every 5-20 seconds.
And keep in mind that this is not an efficient way at all and that this will not work well if there are many clients using your service.

Safely execute queries via AJAX on document ready

Greeting everybody,
My problem is like this: I have some custom made statistics on my internet site where I log informations on what users do ( google analytics - like ). Obviously I aggregate information a couple of months, but the Tables I store information have grown too large and have a negative impact on page loading. The flow is like this ( in index, so affect all pages ) :
1. Get the included files
2. Execute part of statistics queries
3. Effective page code
4. Execute the last part of statistics queries
To get rid of this problem I want to make those queries on <body onload="execQueries();"> or on document ready with javascript / AJAX.
How can I safely and securely make those queries using AJAX, so that cannot be abused by a client with good knowledge of javascript/ajax. Because if I simply make that JS function it can be accesed everytime by a user with firebug.
The solution I think about is including the use of $_SESSION where I mark in top of my index.php information about those queries ( id, info ) and in the script called by AJAX I check if that $_SESSION['query_info'] is set and I execute it reading all the info from there, and then I use unset($_SESSION['query_info']);. So, if the AJAX is called again, because tat specific $_SESSION['query_info'] does not exists, I do not do anything in my DB.
Do you think this is a secure solution or do you have other ideas? Anything viable is welcomed.
Thank you
Try putting your related javascript codes into Closures.

Real Time Event Listener for PHP and mySQL

I am thinking of implementing a real time event listener for a personal project. Is there are way that say, if an INSERT, UPDATE, and DELETE SQL queries have been issued, then mySQL will trigger a PHP file which will in turn process this, like refreshing a page automatically if a new record is found, or say a record has been edited or deleted?
I have been reading through mySQL triggers but I do not know how to implement. Thanks!
If what you want to do is refreshing the page in an appropriately lazy manner, I suggest you look less to triggering out of MySQL and trigger it with AJAX. Waygood has a good link for the latter, but consider the former for simply updating data.
You can update the information on your site by way of long polling. That way you keep a persistant connection open back to your server and can update data whenever the server pushes an update through. When done, simply start the connection again and wait for another update. The most commonly used LP technique is probably one with AJAX. Alternatively, for things like cross-domain support, you could go a bit more exotic with script tag long polling.

Creating a Database that can be updated live

This is very hard to explain but I'm going to try.
We run a motor shop that has a QC program. The program was coded in access97 and it's time for an upgrade, we have elected to try a PHP/MySQL approach to do this.
Right now the access software has several pages to the form and each box sends to the database live so when you type something in you don't have to hit a save button or next or anything and when you come back it's there.
Also the forms are driven by an auto-incremented job number that you can punch into a field at the top of the page and it query's the server and displays all the data in the form boxes so you can edit it.
I don't know how to even start this project. I got a working form and an insert.php page but I don't know how to go about the rest.
If I could get a pointer in the right direction that would be appreciated. Thanks!
You just want it to save automatically? You'll have to look into JavaScript, and more specifically AJAX. I recommend using the jQuery library. Basically, you're going to want to make an AJAX call every time your form field is modified, and that AJAX call will simply update one field in particular.
I understand you are likely very new to website design, so this might be complicated for you.
I would read through this W3Schools tutorial. After reading through that, I'd pay close attention to this tutorial.
Again, this is difficult for beginners. I'd recommend you continue to work at your script, and ask more specific questions here on StackOverflow as time goes on. Good luck!
I have created a simple example here:
HTML/JS:
shaquin.tk/experiments/ajax.html,
PHP: shaquin.tk/experiments/qc.txt.
Have a look at the source to see how it works (I also have some comments in my code), feel free to copy it and modify for your own needs.
To sum up how it works:
When text is typed into a text box, a list of changed elements is updated.
Every updateInterval milliseconds (default 1000), the list is checked. (This helps reduce traffic and lag.) If anything has changed, the PHP file is called to update the database, and the list is cleared.
If an element loses focus and it has changed (e.g. copy/paste), the PHP file is called.
The PHP file sanitizes the query, checks for a valid job number, and updates the database.
References:
AJAX XMLHttpRequest
setInterval
addEventListener
encodeURI
mysqli_connect
mysqli_query
mysqli_real_escape_string
You'll need to submit the data as an ajax request. That way the data can be sent and returned without the page needing to be reloaded to update the information.

PHP and AJAX, sending new PHP info to page for AJAX to receive, is this possible?

I'm searching on how to do this but my searches aren't turning up things that are talking about what I'm trying to do so maybe I'm not searching with the right terms or this isn't possible, but figured I would ask here for help.. this is what I am trying to do..
I have PHP scripts that are called asyncrhonously, so it is called and it just runs, the calling PHP doesn't wait for a response, so it can go on to do other stuff / free things up so another asynch php process can be run.
I would still like to get back a result from these "zombie" scripts or whatever you want to call them, however the only way I can think of doing it that I know for sure will work is something like make this "zombie" script save its final output to a database and then have my AJAX UI make periodic requests to this database to check if the needed value exists in the place it is supposed to.. which would allow it to get the output from the zombie PHP script..
I am thinking it would be better if somehow this zombie script could do a sort of page refresh to the AJAX ui but the ajax ui would intercept this and just take the received data from PHP and use it as needed (such as display in a DIV for user to see).. basically I'm wondering if you can make PHP force this kind of thing rather than needing to involve a database in this and making AJAX do repeated requests to check for a specific value that way..
Thanks for any advice
No, a background script has no way to influence the client's front-end because it has no connection to it.
Starting a background script, having the script write status data into a shared space - be it a database or a memcache or a similar solution - and polling the status through Ajax is usually indeed the best way to go.
One alternative may be Comet. It's a technique where a connection is kept open over a long time, and updated actively from the server side (instead of frequent client-side Ajax polling). I have no practical experience with this but I imagine it most probably needs server side tweaking to be doable in PHP - it's not the best platform for long-running stuff. See this question for some approaches.

Categories