Edit: Just ran into a wrinkle, I just got this working on an iPhone over the weekend and just out of curiosity ran it on two phones at once and it worked. So it ran happily along (as I expected it to the first time) when one phone was an iPhone (iOS 6) and the other was HTC One (Android 4.3). This was however on another person's WIFI. Could my home WIFI be a factor in blocking two devices going to the same location? (The second phone I was using when it didn't work was a Samsung Galaxy Ace 2 (Android 2.3))
So I've got this problem. I am using PhoneGap and coming up with a basic quiz system to muck around with. I've got it working and I'm having a great time. It works essentially through a web console where I can set up a quiz and cycle through questions and with a not so complex series of status updates to an underlying database I use Ajax calls from my mobile to read the status and display a question or wait for the next one.
Basic call is:
$(function checkForQuestion() {
var postData = "stuff needed in called PHP";
$.ajax({
type: "POST",
data: postData,
url: "my called php",
success: function(data){
var result = JSON.parse(data);
if (result == "question") {
--Display Question
} else {
setTimeout(checkForQuestion, 1000);
return false;
}
});
return false;
});
In my limited experience this seems to be a general use of Ajax with a recursive call back if it needs to wait for a new status.
Now this isn't everything by far, but a taster of what I'm doing. It works extremely well with one mobile device and a web console happily working through quizzes.
When I attempt to add a second mobile (haven't even attempted three yet) what happens is the second mobile just silently fails where it is supposed to be calling this ajax. Now the second mobile can quite happily chug along with this on it's own so it shouldn't be a hardware issue. There are no errors thrown out in the PHP, and with a little logging I can't find anything obviously failing.
At this point I thought, perhaps there is a limit on connections on my web host (I'm just using a shared server web host I'm using to test this out) and there are but that is limited to 20 entry processes with Cloud Linux LVE. However This should not preclude two mobiles calling the same PHP (via wifi) at roughly the same time. It should just run the calls concurrently and one will return slight faster than the other (or that is my understanding).
I thought perhaps this is a lock put on the MySQL database I'm using not allowing multiple users/PHP to query it at roughly the same time, but the consecutive nature of PHP should rule that out and some Googling assures me that calls will be queued.
I'm guessing that what is happening is the two calls come in and without returning any error one silently conflicts with the other causing a fail, but I have no idea hwo to find the fail and fix it.
Any suggestions?
Request for the actual code:
$(function () {
$(document).on("pagebeforeshow", "#page6", function () {
document.getElementById("mob_user_name2").innerHTML = window.localStorage.getItem("mob_local_login_name");
$(function checkForQuestion() {
//sort out the data to be posted
var postData = "mob_quizwait_quizcode=" . concat(window.localStorage.getItem("mob_local_quiz_code"), "&mob_quizwait_email=", window.localStorage.getItem("mob_local_login_email"), "&mob_quizwait_password=", window.localStorage.getItem("mob_local_login_password"), "&mob_quizwait_questionid=", window.localStorage.getItem("mob_question_id"));
$.ajax({
type: "POST",
data: postData,
url: "url of php",
success: function(data){
var mob_quizwait_data = JSON.parse(data);
if (mob_quizwait_data.mob_quizwait_success == "mob quizwait go") {
window.localStorage.setItem("mob_question_id", mob_quizwait_data.mob_quizwait_questionid);
window.localStorage.setItem("mob_question_score", mob_quizwait_data.mob_quizwait_score);
window.localStorage.setItem("mob_question_category", mob_quizwait_data.mob_quizwait_category);
window.localStorage.setItem("mob_question_question", mob_quizwait_data.mob_quizwait_question);
window.localStorage.setItem("mob_answer1", mob_quizwait_data.mob_quizwait_correct);
window.localStorage.setItem("mob_answer2", mob_quizwait_data.mob_quizwait_wrong1);
window.localStorage.setItem("mob_answer3", mob_quizwait_data.mob_quizwait_wrong2);
window.localStorage.setItem("mob_answer4", mob_quizwait_data.mob_quizwait_wrong3);
$.mobile.changePage("#page7", {transition:"slide", changeHash:false});
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait stay") {
setTimeout(checkForQuestion, 1000);
return false;
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait intermission") {
document.getElementById("mob_intermission").innerHTML = "Intermission";
$.mobile.changePage("#page6", {transition:"none", changeHash:false});
} else if (mob_quizwait_data.mob_quizwait_success == "mob quizwait finish") {
$.mobile.changePage("#page8", {transition:"slide", changeHash:false});
} else {
navigator.notification.alert("Status Check Failed. Please Try Again.", function(){}, "Alert", "OK");
}
}
});
return false;
});
});
});
And the PHP:
<?PHP
include '../open.php';
//take in POST variables
$quizcode = $link->real_escape_string($_POST["mob_quizwait_quizcode"]);
$email = $link->real_escape_string($_POST["mob_quizwait_email"]);
$password = $link->real_escape_string($_POST["mob_quizwait_password"]);
$questionid1 = $link->real_escape_string($_POST["mob_quizwait_questionid"]);
$quizid = 0;
$status = "X";
$questionid = 0;
$sql = "SELECT QUIZ_ID FROM B_QUIZ WHERE QUIZ_CODE = '$quizcode'";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$quizid = $row['QUIZ_ID'];
}
$sql = "SELECT USER_ID FROM A_USER WHERE USER_EMAIL = '$email' AND USER_PASSWORD = '$password'";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$userid = $row['USER_ID'];
}
$sql = "SELECT QUIZ_STATUS, IFNULL(QUESTION_ID, 0) AS QUESTION_ID FROM B_GAME WHERE QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$status = $row['QUIZ_STATUS'];
$questionid = $row['QUESTION_ID'];
}
if ($questionid == $questionid1) {
$questionid = 0;
}
if ($status != "F") {
if ($quizid != 0 && $status != "X") {
//get details to be written to the profile page
if ($questionid != 0) {
$sql = "SELECT SCORE FROM B_PARTICIPANT WHERE USER_ID = $userid AND QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$score = $row['SCORE'];
}
$sql = "SELECT b.QUESTION_ID, c.CATEGORY, b.QUESTION, b.CORRECT_ANSWER, b.WRONG_ANSWER_1, b.WRONG_ANSWER_2, b.WRONG_ANSWER_3
FROM B_GAME a, B_QUESTION b, D_CATEGORY c
WHERE a.QUESTION_ID = b.QUESTION_ID
AND b.CATEGORY_ID = c.CATEGORY_ID
AND a.QUIZ_ID = $quizid";
$res = $link->query($sql);
while ($row = $res->fetch_array()) {
$questionid = $row['QUESTION_ID'];
$category = $row['CATEGORY'];
$question = $row['QUESTION'];
$correct = $row['CORRECT_ANSWER'];
$wrong1 = $row['WRONG_ANSWER_1'];
$wrong2 = $row['WRONG_ANSWER_2'];
$wrong3 = $row['WRONG_ANSWER_3'];
}
if ($status == "R") {
$arr = array("mob_quizwait_success" => "mob quizwait go",
"mob_quizwait_questionid" => $questionid,
"mob_quizwait_score" => $score,
"mob_quizwait_category" => $category,
"mob_quizwait_question" => $question,
"mob_quizwait_correct" => $correct,
"mob_quizwait_wrong1" => $wrong1,
"mob_quizwait_wrong2" => $wrong2,
"mob_quizwait_wrong3" => $wrong3);
echo json_encode($arr);
} else if ($status == "N") {
$arr = array("mob_quizwait_success" => "mob quizwait stay");
echo json_encode($arr);
} else if ($status == "I") {
$arr = array("mob_quizwait_success" => "mob quizwait intermission");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait stay");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait failed");
echo json_encode($arr);
}
} else {
$arr = array("mob_quizwait_success" => "mob quizwait finished");
echo json_encode($arr);
}
include '../close.php';
?>
open.php:
<?PHP
//DATABASE DETAILS//
$DB_ADDRESS = "yeah";
$DB_USER = "not";
$DB_PASS = "gonna";
$DB_NAME = "do it";
//Connect to the MySQL database
$link = new mysqli($DB_ADDRESS, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
}
?>
close.php:
<?PHP
mysqli_close($link);
?>