Showing Data in MySQL Database In Realtime - php

I am building a weather station that will update the weather in "real-time" to a MySQL Database. To give the feel of "real-time" without taking up a ton of space on my MySQL server, I have my weather station updating a single cell in the MySQL database every few seconds. I would like to set up a way for a website to view this change in realtime without refreshing the page. I have tried researching a lot, but I can't quite find what I am looking for (at least with more of an explanation into the JQuery and how it works). Most answers have partial as people have a lot more experience.
I have a little experience with PHP and MySQL (I have just started learning a couple of months ago). This project is more of a way to learn JQuery and AJAX (along with PHP and MySQL). Frankly, I don't know a ton as I am just starting into programming.
If I could get a bit more of an explanation into how the JQuery works and what files I need to be placing the information in, that would be great (a lot of other similar answers had a separate "server" file and a "client" file in PHP. Do I really need that?).
I just don't know where to begin or what to put in what files. Maybe I need a tutorial? Thanks.

I think what you are looking for is "Push Technology". But, as a beginner like me, I would introduce you to try out "long pulling". Although is not the best method, you can learn how real-time update works.
I suggest you create a normal working AJAX, then use the code below to repeatedly call for updates from the server.
jQuery(document).ready(function () {
interval = setInterval("checkNewUpdate()",4000); //Set interval for accident checking ajax
}
This function basically retrieves information from the database every 4seconds. This method is not so efficient, but I hope would help you to kick start in push technology.
Visit How do I implement basic "Long Polling"? for more information.

I suggest you to create a function to check database status, something like count row in the database or something else like that... the return must be simple a data like number , true or false (not a long data) with interval .. and when it change the fetch function will run

Related

How bad it is to copy paste my own code blocks?

! Actually, I am learning PHP from last couple of months and now I am in a stage where I can program small things like a simple Login Page in PHP and mySQL or a Contact Form. I have wrote a lot of codeblocks like inserting something into a database or selecting something from a database etc. etc. But, I always copy paste my own code-blocks from previous projects while working on a new one. So, I want to know whether this tendency is unique to me only or each of the beginner passes through the same phase once during their journey of being a developer?
Please bear with me because I know this isn't really a programming question and doesn't worth your time as well. I tried finding out in Google as well but this is a snap of what I found:
I mean to say that most of the search results dealt with copy pasting other's code which is not the case of what I am talking about. In order to save time I do copy paste my own code blocks almost everytime. So, how bad is this behaviour of mine?
I again apologize for not posting a question that is worth your time but I am finding it hard to learn to code by myself without having any mentor nearby ( Actually, I searched for a mentor who could teach PHP before giving it a start all by myself, but I found none in my area ) for clearing my doubts and as such Internet is the thing which I mostly depend upon for learning about anything.
This question probably belongs on https://softwareengineering.stackexchange.com but I'll try to give you a decent answer and some guidance.
People re-use their own code all the time. You do not however want to copy/paste if possible. The issue with copy/paste is when you have something used more than a few times - like a MySQL database connection - and it needs updating. I'd rather modify one file (or one small group of files) and have all of my webapps fixed/updated than having to modify 2 or 3 database calls in 9 different web apps...
For things that I use everywhere/all the time - talking with our course management systems API, authenticating a user against our LDAP server, connecting to a MySQL database and running queries, processing forms that are emailed, etc - I've built up my own (or coworkers have) sets of functions, classes, etc. Which I then keep in a single directory, and can include as needed.
If you do this, you want your functions/object methods to be as generic as possible - for example, my MySQL query function takes several arguments - an associative array with connection info (since we have several DB servers based on purpose), a query, and an array of parameters. It returns an array with a status code, and then appropriate data - the record set result for inserts, the ID of the last insert, the count of rows affected (for delete/update). This one function handles 50+ queries and connects to 4 different MySQL servers.

PHP store some data in mySQL for x hours (limited time)

How is it possible to store data in the database for a limited time (like for example 1 hour)?
A user searches a certain thing, the logic is executed server-side and the search result is loaded. As the result is loaded, I want to store it in MySQL and keep it only x hours. After x hours this data should be deleted from the database.
How is it possible to do this in Laravel or in PHP (It does not matter for me. I just mentioned about Laravel as it may have some libraries for this)? Is it about writing logic in SQL or it's a PHP task?
How to do this? Any suggestions/links/solutions?
Thank you very much!
Is it about writing logic in SQL or it's a PHP task?
Doing this with php would be a hassle because you would always need to send requests to check if a record is expired and then send another to delete. This can be achieved using events in mysql. I came across this blog post which might be helpful.
Also dont forget to check the mysql documentation page
Tag records with an expiration timestamp, and have a cron job run every minute (or whatever frequency you prefer) to delete expired records.
The best solution for this task would be to use the Redis database. There are 2 drivers for PHP: pRedis and phpRedis.
The pRedis is a implementation written in PHP which works relatively very slow for a big number of users.
The phpRedis driver is written in C, and it works much faster.
Use the $redis->psetex($key, $destroy_time, $value) function to create self-destroying records. Hope it helps anyone in future

How to deal with External API latency

I have an application that is fetching several e-commerce websites using Curl, looking for the best price.
This process returns a table comparing the prices of all searched websites.
But now we have a problem, the number of stores are starting to increase, and the loading time actually is unacceptable at the user experience side. (actually 10s pageload)
So, we decided to create a database, and start to inject all Curl filtered result inside this database, in order to reduce the DNS calls, and increase Pageload.
I want to know, despite of all our efforts, is still an advantage implement a Memcache module?
I mean, will it help even more or it is just a waste of time?
The Memcache idea was inspired by this topic, of a guy that had a similar problem: Memcache to deal with high latency web services APIs - good idea?
Memcache could be helpful, but (in my opinion) it's kind of a weird way to approach the issue. If it was me, I'd go about it this way:
Firstly, I would indeed cache everything I could in my database. When the user searches, or whatever interaction triggers this, I'd show them a "searching" page with whatever results the server currently has, and a progress bar that fills up as the asynchronous searches complete.
I'd use AJAX to add additional results as they become available. I'm imagining that the search takes about ten seconds - it might take longer, and that's fine. As long as you've got a progress bar, your users will appreciate and understand that Stuff Is Going On.
Obviously, the more searches go through your system, the more up-to-date data you'll have in your database. I'd use cached results that are under a half-hour old, and I'd also record search terms and make sure I kept the top 100 (or so) searches cached at all times.
Know your customers and have what they want available. This doesn't have much to do with any specific technology, but it is all about your ability to predict what they want (or write software that predicts for you!)
Oh, and there's absolutely no reason why PHP can't handle the job. Tying together a bunch of unrelated interfaces is one of the things PHP is best at.
Your result is found outside the bounds of only PHP. Do not bother hacking together a result in PHP when a cronjob could easily be used to populate your database and your PHP script can simply query your database.
If you plan to only stick with PHP then I suggest you change your script to index your database from the results you have populated it with. To populate the results, have a cronjob ping a PHP script that is not accessible to the users which will perform all of your curl functionality.

Mysql Feed to make tables in html/php

Hello all i have a quick question
Is there a way i could make a php system that could read a mysql database and make multiple tables based on that and new information?
So say if we implemented it and we added new information to the database it would automatically add a new table into the php page or would that have to be done manually
I just wanted to know if it is doable before i start to look deeper into the code of it
Yes, it is doable.
There is nothing that can prevent you from doing that.
Taking into account your comment:
Yes it's still doable. Terribly inefficient but doable. You can use AJAX or COMET to update information on your page.
Yes.
Look at phpMyAdmin for an example of what you are talking about.

Live tracking session variables from db

I've been looking for an easy way to track my current session variables on my desktop in order to have an eye on them.
My website uses Joomla and session variables are stored in MySQL by PHP.
What I'd like to achieve is a 'widget' or 'snippet' which could show on my Windows 7 desktop current session variables, and how they change during a visit on the website.
In fact the session state is stored in a table like:
session_id varchar(32)
data varchar(20480)
userid int(11)
So let say one solution could be retrieving first two columns by my userid with a SQL query and parsing 'data' column, which is in JSON format, into an 'snippet' object.
I don't know how to write widget/snippets and I don't expect anybody to write me one, I just wonder if such thing exist to make my developing easier.
Well, if you want to hack together something, I've got a couple of crude suggestions.
Write a simple SQL query ordered by date, throw in some HTML and add javascript to auto refresh the page, say every 30 seconds. Now you just need a tab open to monitor your sessions.
The same thing as above but instead of HTML, output XML in the feed format. Then use any feed reader software which usually has desktop widgets and alerts etc.
Good luck!
Well, it's been long time since I asked this question. So far, the best option I've found was PHP Quick Profiler tunned a bit up for Joomla Framework. Soon, I gonna post a tutorial how to implement this for Joomla. Should anybody want to hurry me up do not hesitate :)

Categories