简体   繁体   English

将Ms Access数据存储到javascript数据集中

[英]Store Ms Access data into javascript dataset

I have a code that fetch all the data in MS Access database in php and its working. 我有一个代码,可以在php中获取MS Access数据库中的所有数据并正常工作。

$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails"; 
$rs = odbc_exec($conn,$sql);
if (!$rs)
   { exit ("Error in Sql");}
    echo "<table><tr>";
    echo "<th>id</th>";
    echo "<th>year</th>";
    echo "<th>month</th>";
    echo "<th>empName</th>";
    echo "<th>empPos</th>";
    echo "<th>numMc</th>";
    echo "<th>numLeave</th></tr>";
    while (odbc_fetch_row($rs))
    {
    $id = odbc_result($rs,"id");
    $year = odbc_result($rs,"year");
    $month = odbc_result($rs,"month");
    $empName = odbc_result($rs,"empName");
    $empPose = odbc_result($rs,"empPos");
    $numMc = odbc_result($rs,"numMc");
    $numLeave = odbc_result($rs,"numLeave");
    }
    odbc_close($conn);
    echo"</table>";
    ?>

But now I need to store all the data in the dataset something like this: 但是现在我需要将所有数据存储在数据集中,如下所示:

var pivot_dataset = [
{"id": 1, "year": 2014 , "month": "JAN", "empName": "David", "empPos": "engineer", "numMc": 1, "numLeave": 2},
];

How to do this?please advise. 怎么做?请指教。

Take a look at the json_encode method. 看一下json_encode方法。 It returns a JSON string if you pass it an associative array. 如果将其传递给关联数组,它将返回JSON字符串。

<?php

$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails"; 
$rs = odbc_exec($conn, $sql);

if (!$rs)
   exit ("Error in Sql");

echo "<table><tr>";
echo "<th>id</th>";
echo "<th>year</th>";
echo "<th>month</th>";
echo "<th>empName</th>";
echo "<th>empPos</th>";
echo "<th>numMc</th>";
echo "<th>numLeave</th></tr>";

$pivot_dataset = array();
while(odbc_fetch_row($rs)) {
    // Push this data onto the end of the array
    $pivot_dataset[] = array(
        'id' => odbc_result($rs,"id"),
        'year' => odbc_result($rs,"year"),
        'month' => odbc_result($rs,"month"),
        'empName' => odbc_result($rs,"empName"),
        'empPose' => odbc_result($rs,"empPos"),
        'numMc' => odbc_result($rs,"numMc"),
        'numLeave' => odbc_result($rs,"numLeave")
    );
}

odbc_close($conn);
echo "</table>";

$jsonStr = json_encode($pivot_dataset);

// var pivot_dataset = [{"id": 1, "year": 2014 , "month": "JAN" ... }, { ... }];
echo "var pivot_dataset = $jsonStr;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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