gpt4 book ai didi

javascript - 从php中的每个位置访问变量

转载 作者:行者123 更新时间:2023-11-29 18:35:22 25 4
gpt4 key购买 nike

我尝试使用谷歌图表API绘制图表。该图表将从数据库中获取数据。用户可以确定他想要绘制图表的日期每周、每年、Mysql 每月 ...等等

当用户选择周、月或年时,MySQL 查询无法访问该变量

错误表明sql查询为空,因为无法访问变量$sql

session_start();
$data_set = $_SESSION['config'];
include("inc/users/$data_set.php");

include('database.php');
$connection = new database();
$con2 = $connection->connect($db, $user, $pass, $host);

$time = $_POST['tar'];

if ($time == 'week') {
$sql = "SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM `radacct` where date(AcctStartTime) = curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND date(AcctStartTime) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY";
} else if ($time == 'month') {
$sql = "SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM radacct
WHERE YEAR(AcctStartTime) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(AcctStartTime) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)";
} else if ($time == 'year') {
$sql = "SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM radacct
WHERE YEAR(AcctStartTime) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)";
}
$result = $con2->query($sql);

while ($result2 = $result->fetch_assoc()) {
$rows[] = array("c" => array("0" => array("v" => $result2['date1'], "f" => NULL), "1" => array("v" => (int) $result2['count'], "f" => NULL)));
}
echo $format = '{
"cols":[
{"label":"date1","pattern":"","type":"string"},
{"label":"count","pattern":"","type":"number"}
],
"rows":' . json_encode($rows) . '}';

最佳答案

缺少您的 else 条件。我添加了 $time 检查条件,但需要添加其他部分。

 session_start();
$data_set=$_SESSION['config'];
include("inc/users/$data_set.php");
include('database.php');
$connection=new database();
$con2=$connection->connect($db,$user,$pass,$host);
$time= $_POST['tar'];

if(!empty($time)){
if($time=='week') {
$sql="SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM `radacct` where date(AcctStartTime) = curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND date(AcctStartTime) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY";
} else if($time=='month') {
$sql="SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM radacct WHERE YEAR(AcctStartTime) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(AcctStartTime) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)";
} else if($time=='year') {
$sql="SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date1` FROM radacct WHERE YEAR(AcctStartTime) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)";
}
} else {
//you have to write default query here
$result = $con2->query($sql);
while($result2= $result->fetch_assoc()) {
$rows[]=array("c"=>array("0"=>array("v"=>$result2['date1'],"f"=>NULL),"1"=>array("v"=>(int)$result2['count'],"f" =>NULL)));
}

echo $format = '{
"cols":[
{"label":"date1","pattern":"","type":"string"},
{"label":"count","pattern":"","type":"number"}
],
"rows":'.json_encode($rows).'}';

关于javascript - 从php中的每个位置访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351898/

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