简体   繁体   中英

Can not echo data from MYSQL database

在此处输入图片说明

Hello! The data in table is like that, and I cannot echo these entries individually. I want to show this data in HTML table like for example, Subject: Math, Correct: 34, Wrong: 6.

I am new in php, please help me with this case.

The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode() then print that object using print_r() .

When put all together, you get

echo print_r(json_decode($data), true);

Note, I have to edit this response back to what it is now because @fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.

@fenil-shah, my answer was written the way it was on purpose. Here are the explanations:

  1. In general, if you want to stay in control of the result of the print_r() function, you need to return its result as a string. That's why I have passed true as second parameter for the print_r() function. You wouldn't using print_r() without returning its result as a string in a middle of another script because it will output some text in an uncontrolled manner that can break an add. Once you have the return string, you can send it to a log file or do whatever you want with it.
  2. json_decode works beautifully without passing it a true as second parameter. The OP's example is showing a object. You edit was purposely changing the decoded object to an array with associative array. Why?
  3. You have removed the space after the comma which you had moved in from the print_r() to the json_decode() to make it look like json_decode($data,true) . You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls ).

php中有echo ""print_r()print()函数可用于打印数据。如果查询返回数组,则使用print_r()函数,但是如果查询未返回任何内容或php中的无效提取方法,则数据无法获取通过任何这些功能显示。

I think this is what you want as a output. You can decode the JSON received from database using json_decode() function and get your result transformed in associative array. Later on passing the array in foreach() loop will do your work accordingly

 <?php
    $result = '{
      "30": {
      "subject_id":343,
      "correct_answers":34,
      "wrong _answers":61,
      "not_answered":0,
      "time_spent":3801,
      "time_to_spend":3680,
      "time_spent_correct_ answers":3286,
      "time_spent_wrong_answers":515 
      },
      "52": {
        "subject_id":52,
        "correct_answers":7,
        "wrong_answers":3,
        "not_answered":0,
        "time_spent":883 ,
        "time_to_spend":94343,
        "time_spent_correct_ans wers":352,
        "time_spent wrong_answers":441
      },
      "53": {
        "subject_id":53,
        "correct_answers":3,
        "wrong_answers":7,
        "not_answered":43,
        "time_spent":584 ,
        "time_to_spend":900,
        "time_spent_correct_ans wers":154,
        "time_spent wrong_answers":430
      }
    }';
    $json_decoded_data = json_decode($result,true);
    ?>
    <table>
        <tbody>
            <tr>
                <th>subject_id</th>
                <th>correct_answers</th>
                <th>wrong_answers</th>
            </tr>
            <?php
            foreach($json_decoded_data as $row){
                echo "<tr>";
                echo "<td>".$row['subject_id']."</td>";
                echo "<td>".$row['correct_answers']."</td>";
                echo "<td>".$row['correct_answers']."</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM