gpt4 book ai didi

php - 使用 PHP 从 MySQL 数据生成 Highcharts 不起作用

转载 作者:行者123 更新时间:2023-11-29 05:59:02 25 4
gpt4 key购买 nike

非常感谢您的帮助。

我正在尝试根据我存储在名为“log”的 mysql 数据库中的数据生成 highcharts 折线图,该数据库有不同的表,每个表有 5 个字段:fecha_id(类型日期)hora_id(类型时间)ping_id(类型 float) ) timechar_id(varchar 类型)和 pingchar_id(varchar 类型)。我尝试使用 POST 方法选择表格、日期和时间行,然后生成有效数据供 highcharts 使用,我尝试将时间用作 X 轴,将 ping 值用作 y轴。正如 Deep3015 所建议的,我编辑了我的代码以遵循 highchart 网站上发布的指南,(更新的)代码是:

<?php
require('conexionBD.php');
$depar = $_POST['dto'];
$date = $_POST['fecha'];
$ini = $_POST['hini'];
$fin = $_POST['hfin'];
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts test</title>

<style type="text/css">

</style>
</head>
<body>
<script src="code/highcharts.js"></script>
<script src="code/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

<script type="text/javascript">
<?php
$sql = "select * from ".$depar" where fecha_id = '".$date"' and hora_id >= '".$ini"' and hora_id <= '".$fin"'";
$result = $conn->query($sql);
while ($row = mysql_fetch_array($result)) {
$data[] = $row['timechar_id'];
$data1[] = $row['pingchar_id'];
}
?>

Highcharts.chart('container', {
chart: {
type: 'line'
},
title: {
text: 'Ping stats'
},
subtitle: {
text: 'Logged on MySQL'
},
xAxis: {
categories: [<?php echo join($data, ',') ?>]
},
yAxis: {
title: {
text: 'Time in [ms]'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series: [{
name: 'LPZ',
data: [<?php echo join($data1, ',') ?>]
}]
});
</script>
</body>
</html>

我仍然得到一个空白页。所以我想知道我做错了什么。谢谢你帮我。

最佳答案

语法错误

$sql = "select * from ".$depar" where fecha_id = '".$date"' and hora_id >= '".$ini"' and hora_id <= '".$fin"'";
^ ^ ^ ^
Here Here

还有mysql_* 函数正式deprecated 从 PHP 5.5(2013 年 6 月发布)开始。 已<强>removed完全 从 PHP 7.0(2015 年 12 月发布)开始,使用 mysqli_*pdo

为了更好的可读性,您还可以使用 HEREDOC如下图

$sql = <<<EOF
select *
from `$depar`
where `fecha_id` = '$date' and
`hora_id` >= '$ini' and
`hora_id` <= '$fin'
EOF;

/* Above you can also use between
`hora_id` between '$ini' and '$fin'
*/

$mysqli = new mysqli("localhost", "my_user", "my_password", "database");

/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

$output = array();

if ($result = $mysqli->query($sql)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$output[] = $row;
}

/* free result set */
$result->free();
}

/* close connection */
$mysqli->close();

/*some error handling if array is empty*/
if(empty($output)){
/*
Couldn't create plot,
*/
}

在你的 Highcharts 选项中

xAxis: {
// your will get [charid, charid2, charid2, ... ]
categories: <?php echo json_encode(array_column($output,'timechar_id'),JSON_NUMERIC_CHECK); ?>
},

和系列数据

series: [{
name: 'LPZ',

// your will get [id1, id2, id2, ... ]
data: <?php echo json_encode(array_column($output,'pingchar_id'),JSON_NUMERIC_CHECK); ?>
}]

关于php - 使用 PHP 从 MySQL 数据生成 Highcharts 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46632661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com