gpt4 book ai didi

php - 返回mysql数据库的数组

转载 作者:行者123 更新时间:2023-11-29 22:23:05 27 4
gpt4 key购买 nike

我有以下代码,它与其他工作 PHP 语句非常相似。但由于某种原因,这个不返回 JSON。唯一的区别是此代码调用整个数据库。

<?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);
?>

这不会返回任何内容。但是,如果我输入 echo json_encode($row);while 命令内,它将返回数据,但当然,每一行都在单独的括号 {} 中(这是其中的一部分):

{"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...

数据库信息全部显示在一个屏幕上,因此并不大。我不明白为什么将其放入数组的最后一步失败了。

最佳答案

您可以尝试使用 print_r() 进行调试。这应该会显示数组中的内容。

如果 $row 已成功进行 json_encoded,则以下代码应该可以工作。但是..让我们看看您的调试会导致您发现什么。

// ... code

$resultArray = array();
while($row = $result->fetch_object()) {
// Add each row into our results array
$resultArray[] = $row;
}
// debugging output
print_r($resultArray); // <-- is the result of this output what you expect?

echo json_encode($resultArray);

// ... code

关于php - 返回mysql数据库的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30541901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com