Hello to this great community!
I've already learned a lot by reading many of the questions and answers here. So here is my current problem:
I'm currently creating a simple code which includes PHP, MySQL and jQuery for a simple news ticker. The aim is to check the database for changes without the need of reloading the whole webpage. If there is a change in the database a javascript is executed to reload the whole webpage. With this I want to avoid, that the user has to reload the webpage manually.
This works nicely!
But there is one problem: When too many users are on the webpage a "Too many connections"-Mysql-error occurs. I think this happens because of the many parallel running updates()-functions.
Do you have an idea how to optimize this code?
<script>
function updates(){
$('#updates').load('updates.php');
}
setInterval("updates()", 1000);
</script>
<div id="updates"></div>
updates.php
<?php
// Get latest value the database
$result = mysql_query("SELECT update FROM updates_db");
$row = mysql_fetch_object($result);
// Compare the value from the database with the current value which is saved in a session
if($_SESSION['update'] != $row->update) {
// If the values do not match, update the session and reload the whole webpage
$_SESSION['update'] = $row->update;
echo '<script>
location.reload();
</script>'
}