我正在尝试从data.php
文件到javascript文件script.js
调用数组,并且我正在使用此方法$.get
,但是却遇到此错误:Every row given must be either null or an array.
这是data.php中的代码:
<?php
$servername = "localhost";
$database = " ";
$username = " ";
$password = " ";
$connect = mysqli_connect($servername, $username, $password, $database);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
$query = 'SELECT * FROM climate';
$result = mysqli_query($connect, $query);
?>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "
['".$row['time']."',".$row['temp'].",".$row['hum']."],";
}
}
?>
和script.js:
function drawLineColors() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'time');
data.addColumn('number', 'Temperature');
data.addColumn('number', 'Humidity');
$.get("data.php", function(sensors){
data.addRows([sensors]);
let rowIndex = data.getNumberOfRows() - 1; //gets the row index of last row
let lastTime = data.getValue(rowIndex, 0); //gets the first column
let lastTemp = data.getValue(rowIndex, 1); //gets second column
let lastHum = data.getValue(rowIndex,2); //gets third column
$("#tempr > .content").html(lastTemp + " °C");
$("#humr > .content").text(lastHum + " %");
我想通过data.addRows([sensors]);
调用数组