以下答案建议使用mysql_但是我决定切换到PDO以提高安全性。但是,当我应用相同的代码时,我很难让它工作。我必须将while
切换为foreach
以进行PDO。
更新:使用PHP PDO
$sql = "SELECT * FROM userrecords";
$stmt = $conn->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
foreach ($data as $row)
{
$dateArray = explode('-', $row['eventdate']);
$year = $dateArray[0];
$month= $dateArray[1] - 1;
$day= $dateArray[2];
$dataArray[] = "[new Date ($year, $month, $day), {$row['scientificname']}, {$row['category_of_taxom']}]";
}
echo $dataArray;
答案 0 :(得分:0)
基于other question的解决方案,您必须将日期解析为javascript的日期对象格式:
$dataArray = array();
while($row = mysqli_fetch_array($result)) {
$dateArray = explode('-', $row['Date']);
$year = $dateArray[0];
$month = $dateArray[1] - 1; // subtract 1 since javascript months are zero-indexed
$day = $dateArray[2];
$dataArray[] = "[new Date($year, $month, $day), {$row['SpeciesA']}, {$row['Speciesb']}]";
}
因为json_encode函数会在这里打破日期,所以我们必须将它解析为DataTable构造函数有点不同:
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Species A');
data.addColumn('number', 'Species B');
data.addRows(<?php echo '[' . implode(',', $dataArray) . ']'; ?>);