I'm attempting to modify and read a database using AJAX and PHP.
I have a PHP file that is accessed every 2 seconds through jQuery.
I use a GoDaddy web hosting service, if that makes a difference.
When I open the website in 2 pages, the code runs for a while before it returns "not found" errors in the console.
When this happens, the entire web server seems to reboot, taking a minute or two.
I've seen other questions and answers with the same problem, but they have a different cause, and their solution does not apply to me.
I have stripped out irrelevant code for the sake of a faster read, but if you beleive you are missing information, I will supply more or all code I use.
//javascript/jQuery code
$(document).ready(function() {
var id = Math.round(Math.random() * 7000000);
var x = 0;
var y = 0;// x and y are set to the mouse coordinates in code that I cut out.
var db = [];
//sends its own ID to add to the database
$.post('src/insert.php?id=' + id + '&x=' + x + '&y=' + y);
setInterval(function() {
//reads the database, and sends its own ID for SQL to exclude
$.get('src/select.php?id=' + id, function(data) {
db = data;
//db becomes an array of the database table in code that I cut out.
});
//sends its own data to the database through PHP
$.post('src/update.php?id=' + id + '&x=' + x + '&y=' + y);
}, 2000);//this code activates every 2 seconds.
});
.
//PHP code (I have 3 files like this, and I'll explain differences when necessary.)
$hostname = "123mysql123.example.com";
$username = "theuser";
$dbname = "thedatabase";
$password = "nospaces";
//I change this part from each file so it SELECTs, INSERTs, and UPDATEs.
//I've tested these and they work, but they might be crashing from overload.
$query = "SQL CODE WITH " . $_GET['data'] . " FROM GET";
$conn = mysqli_connect($hostname, $username, $password, $dbname);
if(!$conn) {
die("error message");
}
//in files where no return is needed, the query is not stored to a variable.
$result = mysqli_query($conn, $query);
mysqli_close($conn);
//where a return value is needed, here I use mysqli_fetch_array($result) to get the array
//I have more code to put it into string form and echo it.
//the javascript parses it and uses it.
The javascript is fully able to read the database, and having two tabs open proves that they interact with each other. The problem is that they halt after several seconds, and refreshing the page shows that the server is down.
If you have a similar problem, I hope this solves or helps you figure it out.
If you need any more information, please comment nicely. Thanks!