我想在表格中显示以下数据

问题描述 投票:1回答:1

我想在表格中显示以下数据。

我尝试从数据库中获取数据,但只能看到一列

# AJAX CODE

    $.ajax({
        url:'process/getState.php',
        method:'GET',
        success:function(response){
            res = JSON.parse(response);
            console.log(res);
            $.each(res,function(k,v){
                var t = $('.template > table > tbody > tr').clone();
                t.find('.state').html(v.state);
                t.find('.count').html(v.count);

                $('#tbody').append(t);
                console.log(v); 
            });
        }
    })

# PROCESS FILE

<?php
    include('connection.php');

    $conn = connection();
    $sql = "SELECT * FROM statedistribution";
    $result = $conn->query($sql);
    $state = [];
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            array_push($state,$row); 
        }
        die(json_encode($state));
    } else {
        echo "0 results";
    }
    $conn->close();
?>

我只在状态列中获取数据,但计数列结果为空白

javascript php mysql html5
1个回答
0
投票

您看到一个列或行,因为您的$状态未正确声明为数组只需将您的流程文件编辑为如下所示:

include('connection.php');

$conn = connection();
$sql = "SELECT * FROM statedistribution";
$result = $conn->query($sql) or die ("Error :".mysql_error());
$state = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $state[] = $row;
    }
    $output = json_encode($state);
} else $output = "0 results";
$conn->close();
echo $output;
© www.soinside.com 2019 - 2024. All rights reserved.