[英]Why don't I have to decode/parse a JSON string generated by PHP in JS
[英]Don't display values by using Json decode()
我將 HTML 表中的一些值存儲到 MySQL 數據庫中。 它成功地存儲在數據庫中,但是當我使用Json decode()
獲取數據時,Html 表沒有顯示網頁中的任何數據,但沒有發生任何錯誤。
這是php代碼:
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>Medicine Name</th>
<th>Morning</th>
<th>Noon</th>
<th>Noght</th>
</tr>
</thead>
<tbody>
<?php
require_once 'auth/dbconnection.php';
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
?> <tr>
<td><?php echo $object->medname ?></td>
<td><?php echo $object->morning ?></td>
<td><?php echo $object->noon ?></td>
<td><?php echo $object->night ?></td>
</tr>
<?php
} }}}
?>
</tbody>
</table>
這是我的數據庫表
我不知道我哪里錯了。 如何改進代碼段?
請注意,您錯過了 fetch 本身: $result->fetch_assoc()
您使用了 var $row
但從何而來?
我認為你的代碼應該是:
$sql = "SELECT * FROM prescription";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
while ($row = $result->fetch_assoc()) { <---- Notice this line
$medRecords = json_decode($row['med_records'],true);
if (is_array($medRecords) || is_object($medRecords)) {
foreach($medRecords as $key => $object) {
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.