I have a PHP function that I am querying mySQL database and I need to send the data from that query over to a javascript function so that the data can be used in plotly graph. I am able to query the data in PHP and get the information I need but when I try to get the information in my javascript file it says NULL. Here's my code with comments about what I am getting.
PHP function:
function getCourses() {
$conn = getDBConn();
$query = "SELECT course_id FROM TraineeEventCourses GROUP BY course_id;";
$result = mysqli_query($conn, $query) or die('Error connecting to mysql');
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $courseID) {
$course = $courseID;
print_r($course);
echo "<br>"; // Print's 1 2 3 4 8 9 10 as expected
// return $course;
}
}
// print_r($course);
// echo "<br>"; // When not commented out this goes with the return statement but it only returns 10 for some reason instead of returning the whole array.
}
Javascript:
var courses = "<?php echo json_encode($course, JSON_PRETTY_PRINT) ?>";
console.log(courses); // Returns NULL but should be returning 12348910
TESTER = document.getElementById('tester');
Plotly.plot( TESTER, [{
x: [courses],
y: [courses] }],
{ margin: { t: 0 } } );
$course?