I have the following code, which is very similar to other working PHP statements. But for some reason, this one does not return JSON. The only difference is that this code calls for an entire database.
<?php
// Create connection
include_once 'functions.php';
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$state = $_POST["state"];
$db = $state."Standards";
// This SQL statement selects ALL from the table $db
$sql = "SELECT * FROM $db";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object()) {
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
This returns nothing. However, if I put echo json_encode($row);
inside the while
command, it will return the data, but of course in separate brackets {} for each row (here's a part of it):
{"Grade":"1","1CE":"1CE Identify echo and call\/response.","2CE":"2CE Explore steady beat, rhythm and meter.","3CE":"3CE Listen to and identify music of various and contrasting styles, composers, periods and cultures.","4CE":"4CE Identify elements of music using developmentally appropriate vocabulary (e.g., rhythm syllables and solfege). ","5CE":"5CE Explore selected musical instruments aurally and visually.","6CE":"6CE Attend live music performances with emphasis on concert etiquette.","7CE":"","8CE":"","1PR":"1PR Demonstrate echo and call\/response. ","2PR":"2PR Sing (using head voice and appropriate posture) and move to music of various styles, composers and cultures with accurate pitch and rhythm.","3PR":"3PR Read, write and perform using eighth notes, quarter notes and quarter rests. ","4PR":"4PR Improvise new lyrics to known songs and experiment with digital technology.","5PR":"5PR Read, write and perform la-sol-mi melodies on the treble staff in Gdo, F-do and C-do using a system (e.g., solfege, numbers or letters). ","6PR":"6PR Play a variety of classroom instruments, alone and with others, and demonstrate proper technique.","7PR":"7PR Demonstrate audience behavior appropriate for the context and style of music performed.","8PR":"","9PR":"","1RE":"1RE Recognize how music is used for a variety of occasions.","2RE":"2RE Describe how music communicates feelings, moods, images and meaning.","3RE":"3RE Communicate a response to music using dance, drama or visual art.","4RE":"4RE Connect concepts shared between music, other art forms and other curricular subjects.","5RE":"5RE Form and express personal opinions about a m...
The DB info all fits on one screen, so it's not huge. I can't figure out why the final step of putting it into an array is failing.