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

时间:2019-03-25 03:04:50

标签: javascript php mysql html5

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

我尝试从数据库中获取数据,但是只有一列可见

# 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();
?>

我只在状态列中获取数据,但计数列却变成空白

1 个答案:

答案 0 :(得分:0)

您看到的是一列或一行,因为您的$ state未正确声明为数组 只需编辑您的过程文件,如下所示:

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;