Php 无法从MYSQL数据库回显数据
你好!!表中的数据是这样的,我不能单独回显这些条目。我想在HTML表格中显示这些数据,例如,主题:数学,正确:34,错误:6Php 无法从MYSQL数据库回显数据,php,mysql,Php,Mysql,你好!!表中的数据是这样的,我不能单独回显这些条目。我想在HTML表格中显示这些数据,例如,主题:数学,正确:34,错误:6 我是php新手,请帮我解决这个问题。表中的数据似乎是JSON格式的。您可以使用json_decode将其转换为PHP对象,然后使用print_r打印该对象 当所有这些放在一起时,你会得到 echo print_r(json_decode($data), true); 请注意,我必须将此回复编辑回现在的状态,因为@fenil shah已将其更改为其他一些不合适的内容。请发
我是php新手,请帮我解决这个问题。表中的数据似乎是JSON格式的。您可以使用json_decode将其转换为PHP对象,然后使用print_r打印该对象 当所有这些放在一起时,你会得到
echo print_r(json_decode($data), true);
请注意,我必须将此回复编辑回现在的状态,因为@fenil shah已将其更改为其他一些不合适的内容。请发送您自己的回复,而不是让其他人说出他们不想说的话
@费尼尔·沙阿,我的回答是故意写的。以下是解释:
通常,如果您想控制print_r函数的结果,则需要将其结果作为字符串返回。这就是我将true作为print\r函数的第二个参数传递的原因。如果不在另一个脚本中间以字符串形式返回其结果,则不能使用print_r,因为它将以不受控制的方式输出某些文本,这可能会中断add。一旦得到返回字符串,就可以将其发送到日志文件,或者对其执行任何操作。
json_decode工作得很好,没有将其作为第二个参数传递给true。OP的示例显示了一个对象。编辑时故意将解码对象更改为具有关联数组的数组。为什么?
您已经删除了逗号后的空格,该逗号已从print_r移到json_decode,使其看起来像json_decode$data,true。您需要知道,空间的存在有两个原因:可读性和对PSR-2标准的遵从性。
php中有echo、print\r和print函数可用于打印数据。如果查询返回数组,则使用print\r函数,但如果查询未返回任何内容或php中的无效获取方法,则这些函数中的任何一个都无法显示数据。我想这就是您想要的输出。 您可以使用JSON_decode函数对从数据库接收到的JSON进行解码,并将结果转换为关联数组。 稍后,在foreach循环中传递数组将完成相应的工作
<?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>
请以文本而非图像形式发布代码/json。