【问题标题】:Having an array before encoding mysql array在编码mysql数组之前有一个数组
【发布时间】:2019-06-18 23:02:29
【问题描述】:

我正在尝试在 json_encode 中的 mysql 数组之前填充一个验证数组。这是我想要在 mysql 数组之前的数组 "array("status":"true","message":"Data fetched successfully!","data":" 但是当我运行 Web 服务时它只是空白. 有什么想法吗?

<?php

// Create connection
$con=mysqli_connect("localhost","burtkunt_dbuser","phatelives","burtkunt_colors");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM colors";

// 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(array("status":"true","message":"Data fetched successfully!","data":$resultArray));
}

// Close connections
mysqli_close($con);
?>

这是我想要的样子:

{"status":"true","message":"Data fetched successfully!","data":[{"id":"1","name":"Roger Federer","country":"Switzerland","city":"Basel","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/roger.jpg"},{"id":"2","name":"Rafael Nadal","country":"Spain","city":"Madrid","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/nadal.jpg"},{"id":"3","name":"Novak Djokovic","country":"Serbia","city":"Monaco","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/djoko.jpg"},{"id":"4","name":"Andy Murray","country":"United Kingdom","city":"London","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/murray.jpg"},{"id":"5","name":"Maria Sharapova","country":"Russia","city":"Moscow","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/shara.jpg"},{"id":"6","name":"Caroline Wozniacki","country":"Denmark","city":"Odense","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/woz.jpg"},{"id":"7","name":"Eugenie Bouchard","country":"Canada","city":" Montreal","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/bou.png"},{"id":"8","name":"Ana Ivanovic","country":"Serbia","city":"Belgrade","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/iva.jpg"}]}

【问题讨论】:

  • 您的 php 语法无效。
  • (array("status":"true","message":"数据获取成功!","data":$resultArray) 这不是数组

标签: php mysql arrays json


【解决方案1】:

您的array 格式不正确,为

这是正确的格式

$array = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

$json_arr = array("status"=>"true","message"=>"Data fetched successfully!","data"=> $resultArray);

echo json_encode($json_arr);

【讨论】:

    【解决方案2】:

    你必须先选择它,然后像这样在里面的所有数据库中赋值

    这是我从数据库中选择数据时制作 json 的方式

    $tempArray = array();
    
    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
    
        $data = array("id" => $row['id'], "name" => $row['name'],"country" => $row['country']);
        array_push($temparray, $data);
    }
    $arr= array("status"=>"true","message"=>"Data fetched successfully!", "data" => $temparray);
    echo json_encode($arr);
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您的 PHP 数组分配不正确。此外,我发现您的代码还有其他问题。这是一个改进的版本

      <?php
      
      // Create connection
      $con=mysqli_connect("localhost","burtkunt_dbuser","phatelives","burtkunt_colors");
      
      // Check connection
      if (mysqli_connect_errno())
      {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      
      // This SQL statement selects ALL from the table 'Locations'
      $sql = "SELECT * FROM colors";
      
      // 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(); // unnecessary
      
          // 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, $row);
          }
      
          // Finally, encode the array to JSON and output the results
          echo json_encode(array("status" => "true","message" => "Data fetched successfully!","data" => $resultArray));
      }
      
      // Close connections
      mysqli_close($con);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 2018-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多