i've been experiencing a problem recently. A couple of months ago i developed this code and it was working, now i tried running the code again and it is simply not working. I will explain
I have a javascript file which queries data to a php file, that is supposed to query data from 3 tables, A, B, and C in form of an INNER JOIN. Like i said before it was working, and now it simply returns and error to my javascript due to that it returns and empty array and it gives me the JSON.parse error on my javascript.
Ok moving on, Whenever i push data obtained From tables A and B, the php returns a complete array, but if I push the results from table C, my php returns and empty array. I've tried running my sql to check if it is wrong, and my sql queries fine on my console. Its so frustrating because i see that the results are returned on the console form the 3 tables, but if i try to obtain them from my php file, it will return an array if it contains the data from tables A and B, but as soon as i include the results from table C, it simply returns empty.
It has happened to me before but i simply cannot find and answer as to why it is happening because in some files it does work when i query multiple tables as up to 5 tables long but on some it simply wont work.
Here is my php code:
<?php
if(!isset($_POST['Select']))
die("You Do Not Have Permission To Access This File");
require_once 'DB_Connect.php';
$db = new DB_Connect();
$conn = $db->connect();
$sql = "SELECT AssignedStudent.Student_ID, Programs.name, Student.name As StName, Student.email FROM AssignedStudent INNER JOIN Programs ON AssignedStudent.Programs_ID = Programs.ID INNER JOIN Student ON AssignedStudent.Student_ID = Student.ID WHERE AssignedStudent.status='Active' ORDER BY Programs.name, AssignedStudent.Student_ID ASC";
$resultsAssigned = mysqli_query($conn, $sql);
$data = array();
$data2 = array();
if(mysqli_num_rows($resultsAssigned)>0){
while($row = mysqli_fetch_assoc($resultsAssigned)) {
$StProgam = $row["name"];
$StID = $row["Student_ID"];
//$StName = $row["StName"];
//$StEmail = $row["email"];
$StName = "das";
$StEmail = "das";
//echo $StName."-".$StEmail."\n";
array_push($data, array("ProgramName"=>$StProgam,"StudentID" => $StID, "StudentName" => $StName, "StudentEmail" => $StEmail));
//$data[] = array("ProgramName"=>$StProgam,"StudentID" => $StID, "StudentName" => $StName, "StudentEmail" => $StEmail);
}
}
echo json_encode($data);
?>
To close off, if any body is reading this post, please focus on what is inside the While loop because everything else is working 100%, ive already tested it before. Inside the while loop i have some pieces of code commented which demonstrate my error that i was talking about. if I uncomment these two lines: //$StName = $row["StName"]; //$StEmail = $row["email"]; my php will return an empty result to my javascript, but these two lines work fine: $StName = "das"; $StEmail = "das";. As i mentioned before I access data from 3 separate tables, and as soon as i include data from table C, which are the variables that i metioned just now, my php simply wont return any value but as soon as i ommit them, it works just fine.
Only those 2 lines produce me the error, which basically is absurd because i obtain full results from my query, proven on my console, but as soon as i access them from row[] result and push it into the data array, it returns an empty array.
Moreover, i tried echoing the results when i obtain them from the row[] and it does return the results, but as soon as i push them into the array it stops working. To explain it i push $StName = "das"; $StEmail = "das"; into my array it works fine, but if i push $StName = $row["StName"]; $StEmail = $row["email"]; it wont work, yet if i echo the results in these variables, they do show.
The problem is i cant push into my array when the results in $row["StName"]; and $row["email"];
Here is a screen shot of the query from my console:
Lastly, im running the code on my localhost using xampp, i've uninstalled my xampp that was from 3 years ago and updated to the latest version and still have the same error. Its so frustrating. I know it might sound out of context but could it be hardware problem from my laptop?
P.S. This same code with the same database on my online server WORKS COMPLETELY FINE 100% but on my machine local host it gives me and empty array as result.