返回mysql数据库的数组

时间:2015-05-30 03:24:24

标签: php mysql

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

if("method = POST")

这不会返回任何内容。但是,如果我放<?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

数据库信息全部适合一个屏幕,所以它并不大。我无法弄清楚为什么将它放入数组的最后一步失败。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用// ... 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 进行调试。这应该会告诉你数组中的内容。

如果import java.util.InputMismatchException; import java.util.Scanner; public class Homework1a { public static void main(String[] args) { int intOne = 0; int intTwo = 0; String operation = " "; int result = 0; double divResult = 0.0; // Use the Scanner class to input data Scanner scannerIn = new Scanner(System.in); // Display of Program Introduction System.out.println("This program will take your integer inputs, " + "along with the choosen operation and calculate the results"); try { // Display for user to input the first integer System.out.println("Enter the first integer:"); intOne = scannerIn.nextInt(); // Display for user to input the second integer System.out.println("Enter the second integer:"); intTwo = scannerIn.nextInt(); // Display a list of instructions on the proper call for the operator System.out.println(""); System.out.println("********** Operator Instructions **********"); System.out.println("\t For addition use +"); System.out.println("\t For subtraction use -"); System.out.println("\t For multiplication use *"); System.out.println("\t For division use /"); System.out.println("\t For modulus use %"); System.out.println("\t For bitwise AND use &"); System.out.println("\t For bitwise inclusive OR use |"); System.out.println("********************************************"); System.out.println(""); // Takes care of the ENTER key negating the String input below scannerIn.nextLine(); // Display for user to input the chosen operator System.out.println("Enter the operation:"); operation = scannerIn.nextLine(); } catch (InputMismatchException ex) { System.out.println("Your Integer value is out of range."); } } } 成功json_encoded,则以下代码 应该。但是......让我们看看你的调试会让你发现什么。

<pagination boundary-links="true" total-items="totalItems" ng-model="currentPage" class="pagination-sm" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;" items-per-page="1"></pagination>

答案 1 :(得分:0)

当尝试使用编码不正确的文本元素对数组进行编码时,显然json_encode()会无声地失败。我发现here的解决方案是在添加到数组之前对utf8_encode中每个混乱的字符串元素进行编码。

while($row = $result->fetch_object())
{
    // Add each row into our results array (and encode in utf8)
    $tempArray['grade'] = $row->grade;
    $tempArray['category'] = utf8_encode($row->category);
    $tempArray['standard'] = utf8_encode($row->standard);
    $resultArray[] = $tempArray;
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);