So here's the code:
function pullLists(){
require ($_SERVER['DOCUMENT_ROOT'] . '/sql_connection/connect.php');
$sql_sn = "SELECT sName FROM skills";
$sql_st = "SELECT sType FROM skills";
$sql_class = "SELECT class_name FROM class_info";
$sql_race = "SELECT race_name FROM races";
$r_sn = $link->query($sql_sn);
$r_st = $link->query($sql_st);
$classes = $link->query($sql_class);
$races = $link->query($sql_race);
$skillnames = array();
$skilltypes = array();
$classnames = array();
$racenames = array();
if ($r_sn->num_rows > 0){
while ($row = $r_sn->fetch_assoc()){
$skillnames[] = $row["sName"];
}
echo $skillnames[1];
} else {echo "No rows found for Skill Names.";}
if ($r_st->num_rows > 0){
while ($row = $r_st->fetch_assoc()){
$skilltypes[] = $row["sType"];
}
} else {echo "No rows found for Skill Types.";}
if ($classes->num_rows > 0){
while ($row = $classes->fetch_assoc()){
$classnames[] = $row["class_name"];
}
} else {echo "No rows found for Class Names.";}
if ($races->num_rows > 0){
while ($row = $races->fetch_assoc()){
$racenames[] = $row["race_name"];
}
} else {echo "No rows found for Race Names.";}
$resultArray = array($skillnames, $classnames, $racenames);
echo json_encode($resultArray);
}
If I run it as-is, I get an error in my Chrome Console saying "Uncaught SyntaxError: unexpected end of input" and points at my .js file that's calling this function (via AJAX and all that). However, if I remove $skillnames
from $resultArray
, it returns the other two arrays just fine and the .js script works great. I'm baffled. While I was debugging, I added the echo $skillnames[1];
line just to see what would happen, and sure enough in my Chrome Console I saw the 2nd value of the $skillnames
array. So as far as I can tell, it's pulling the values correctly, but by the time it gets down to actually echo
'ing $resultArray
it gets goobered up. What am I missing?
Same "unexpected end of input" error ends up happening when I throw $skilltypes
into $resultArray
. This was working for a while, but I ended up rebuilding the skills table in my SQL database. That's the latest change I remember that might cause this, but I don't see why it would. I'm convinced it's this PHP file, but if y'all would like to see the AJAX that's calling it, let me know.
.js for giggles:
//Pull Skill, Class, & Race Lists
window.onload = function() {
xhr = new XMLHttpRequest();
userVar = "func=pullLists";
xhr.open("POST", "pullSQLData.php", true);
xhr.onreadystatechange = function(){
if (xhr.status == 200 && xhr.readyState == 4){
responseArray = xhr.responseText;
targetDiv = document.getElementById('skillList');
console.log(responseArray);
targetInfo = JSON.parse(responseArray);
// ... do stuff...
}
}
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(userVar);
}
JSON.parse(responseArray);
is as far as it gets. console.log(responseArray);
will either return nothing at all (not even a pair of empty square brackets) if $skillnames
or $skilltypes
are a part of the result from PHP, or will return the correct arrays if the aforementioned arrays are taken out of the result.
Found it. In rebuilding my "skills" table, I used parentheses in some of the names. After iterating through the array list on its own, I saw the Chrome Console throw some weird symbols when it hit those names.
Sure enough, renaming those values and removing the parentheses in the SQL table solved it.