M.Bwe M.Bwe - 2 years ago 79
Javascript Question

access variable from every where in php

I trying to

draw chart using google chart API
. This chart will get data from the database. The user can determine the date that he wants to draw the chart
weekly, year, Mysql monthly
...etc.

when user choose week or month or year MySQL query can not access to this variable

the error says that the sql query is empty because can not access to the variable
$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) . '}';




Answer Source

Your else condition is missing. I add $time check condition but need to add else part.

 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 queery 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).'}'; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download