【问题标题】:How to display data in an object如何在对象中显示数据
【发布时间】:2020-10-14 01:16:05
【问题描述】:

输出数据时,以一行显示。如何确保每条记录都是一个对象,所有对象都变成一个数组?

<?php
  require_once("db.php");
  $query = $db->query('SELECT * FROM  `dictdb`.`dictwords`');
  while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo "
           id: " . $row['id'] . ",
           engWord: " . $row['engwords'] . ",
           rusWord: " . $row['ruswords'] ."
          ";
  }
?>

test.js

$(document).ready(function () {
    $(".btn-test").on("click", function () {
        $.ajax({
            url: "/src/php/tests.php",
            type: "POST",
            success: function (data) {
              $(".test-word").html(data);
            }
        })
    })

【问题讨论】:

    标签: javascript php mysql arrays ajax


    【解决方案1】:

    将所需的 HTML 标记放入您回显的数据中。

    echo "<br>
           id: " . $row['id'] . ",<br>
           engWord: " . $row['engwords'] . ",<br>
           rusWord: " . $row['ruswords'] ."<br>
          ";
    

    【讨论】:

      【解决方案2】:

      您的 echo 不会生成 JSON 对象。您需要执行以下操作:

      <?php
      require_once("db.php");
        
      $query = $db->query('SELECT * FROM  `dictdb`.`dictwords`');
      
      $response = [];
      
      while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
          $response[] = [
              'id' => $row['id'],
              'engWord' => $row['engwords'],
              'rusWord' => $row['ruswords'],
          ];
      }
      
      echo json_encode($response);
      

      这将产生一个 JSON 对象,类似于(如果您从 DB 返回 2 个结果):

      [
          {
              id: 'YourID',
              engWord: 'Your Eng Word',
              rusWord: 'Your Rus Word',
          }, {
              id: 'YourID',
              engWord: 'Your Eng Word',
              rusWord: 'Your Rus Word',
          }, 
      ]
      

      我强烈建议您使用 JSON 响应,而不是尝试解析 HTML。

      【讨论】:

        【解决方案3】:

        因为您将纯文本插入 HTML。这意味着&lt;tags&gt; 忽略您的空格(如换行符)。

        要么从 PHP 加载一些合理的 HTML

        ## in PHP
        ...
        echo "<p>
          id: " . $row['id'] . ",<br>
          engWord: " . $row['engwords'] . ",<br>
          rusWord: " . $row['ruswords'] ."<br>
        </p>";
        ...
        

        或者,更好的是,使用 JSON

        ## in PHP
        ...
        $results = [];
        while ($row = $query->fetch(PDO::FETCH_ASSOC))
          $results[] = $row;
        
        header('Content-Type: application/json');
        echo json_encode($results);
        ...
        
        ## in JS
        $.ajax('/src/php/tests.php')
        .done(data => { // use .success for older jQuery versions
          let formatedJSON = JSON.stringify(data, null, 2);
          $(".test-word").html(`<pre>${formatedJSON}</pre>`);
        });
        
        
        

        【讨论】:

        • 如何访问特定对象? $ (".test-word")。 html(格式化JSON [2]);不起作用。谢谢
        • @Aleksf, formatedJSON 在这里已经是一个字符串。对于对象的特定属性,使用原始对象和属性的键,如data.iddata.engWord 等... 示例:$(".test-word").html(data.engWord)$(".test-word").html("English: " + data.engWord + ", Russian: " + data.rusWord);
        【解决方案4】:

        当您使用函数查询时,您要求在数据库上执行查询。因此,要查看读取查询时返回的内容,您应该检查该函数的文档,但我很确定它是一个对象,因为当您执行 $query->fetch 时,您正在使用该对象的方法。

        相反,对于数组的问题,只需尝试使用 try-catch(我不擅长重复)处理异常块,看看,如果它在转换为数组时给你一个错误,那么这意味着你不能转换将这些数据放入一个数组中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-07
          • 1970-01-01
          • 2022-06-23
          • 2021-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多