I'm new on this forum and I have a question related to MySQL database. Let's say I have a JS script which access via Ajax a PHP file. The file updates and returns the number of online users (it executes 4 querys). Considering JS script calls the PHP one every 1 second (+ Time/Latency - 1,0X s / 1,0X s), can my method cause a crash to the database if too many users are online at a time and so, too many querys are executed?
My JS file:
$(document).ready(function(){
var logout = false;
setInterval(checkUsers,1000);
$("a.logout").click(function(){
logout = true;
});
function checkUsers(){
if(logout) return;
$.get("uo.php",function(data){
$("p#uo").html("Users online: <b>" + data + "</b>");
});
}
});
My PHP file:
<?php
// Block non-Ajax access
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
session_start();
define('INCLUDE_CHECK',true);
require_once 'includes/config.php';
$time = time();
$timeout = $time - 10;
$user = $_SESSION['user'];
$ps = $pdo->query("SELECT COUNT(*) FROM `online` WHERE `user` = '". $user ."'");
$status = (bool) $ps->fetchColumn(0);
if($status) $pdo->query("UPDATE `online` SET `timestamp` = ". $time ." WHERE `user` = '". $user ."'");
else $pdo->query("INSERT INTO `online`(`user`,`timestamp`) VALUES('". $user ."',". $time .")");
// Delete records older than 10 seconds
$pdo->query("DELETE FROM `online` WHERE `timestamp` < ". $timeout ."");
$ps = $pdo->query("SELECT COUNT(*) FROM `online`");
$onlineNum = (int) $ps->fetchColumn(0);
echo $onlineNum;
}else header('Location: index.php');
Sorry if my question seems to be a little un-professional, but I'm an amateur programmer and I'm still learning

New Topic/Question
Reply



MultiQuote






|