两个PHP数组没有返回,两个是

时间:2021-11-28 07:37:40

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?

如果我按原来的方式运行,我的Chrome控制台会出现一个错误:“未捕获的SyntaxError:意外的输入端”和指向我的.js文件的点(通过AJAX和所有这些)。但是,如果我从resultArray中删除$skillname,它将返回其他两个数组,而.js脚本工作得很好。我困惑。在调试期间,我添加了echo $skillnames[1];行,看看会发生什么,当然,在我的Chrome控制台,我看到了$skillnames数组的第二个值。就我所知,它正在正确地提取值,但是当它真正地返回到返回$resultArray时,它就会被粘附起来。我缺少什么?

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.

当我将$skilltypes放入$resultArray时,同样的“输入的意外结束”错误结束。这工作了一段时间,但最终我重新构建了SQL数据库中的技能表。这是我所记得的可能导致这种情况的最新变化,但我不明白为什么会这样。我确信是这个PHP文件,但是如果你想看到AJAX调用它,请告诉我。

.js for giggles:

. js咯咯地笑:

//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.

JSON.parse(responseArray);到此为止。console.log(responseArray);如果$skillnames或$skilltypes是PHP结果的一部分,则将不返回任何内容(甚至连一对空方括号都不返回),如果上述数组从结果中取出,则返回正确的数组。

1 个解决方案

#1


0  

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.

找到了。在重新构建“技能”表时,我在一些名称中使用了括号。在自己遍历数组列表之后,我看到Chrome控制台在碰到这些名称时抛出了一些奇怪的符号。

Sure enough, renaming those values and removing the parentheses in the SQL table solved it.

确实,重命名这些值并删除SQL表中的括号解决了这个问题。

#1


0  

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.

找到了。在重新构建“技能”表时,我在一些名称中使用了括号。在自己遍历数组列表之后,我看到Chrome控制台在碰到这些名称时抛出了一些奇怪的符号。

Sure enough, renaming those values and removing the parentheses in the SQL table solved it.

确实,重命名这些值并删除SQL表中的括号解决了这个问题。