0

Hi im new to php and trying to explore! Im having a problem inserting my variable to my code. Heres my variables

 $jun = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'June' and YearCreated = '2016';");
 $jul = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'July' and YearCreated = '2016';");
 $aug = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'August' and YearCreated = '2016';");
 $sep = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'September' and YearCreated = '2016';");
 $oct = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'October' and YearCreated = '2016';");
 $nov = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'November' and YearCreated = '2016';");
 $dec = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'December' and YearCreated = '2016';");

And here is where im trying to put it:

$(function () {
    $('#container').highcharts({
    title: {
        text: 'Monthly Average Requests',
        x: -20 //center
    },
    subtitle: {
        text: 'Source: Barangay Harapin ang Bukas',
        x: -20
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis: {
        title: {
            text: 'Volume'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        valueSuffix: '' //suffix *C
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle',
        borderWidth: 0
    },
    series: [{
        name: 'Complaints',
        data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9,20]
    }, {
        name: 'Services',
        data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
    }, {
        name: 'Registered Constituents',

Here exactly is where im tyring to put my variables. Cant figure this out.

    data: [$jul,$aug]

    }]
});
});
chris85
  • 23,846
  • 7
  • 34
  • 51
Jobet Avila
  • 23
  • 1
  • 4
  • You need to fetch the result object. Also `$jul` is not PHP unless you enter the PHP block and output it ``. – chris85 Aug 03 '16 at 12:31

2 Answers2

0
$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'June' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$jun = $row['count'];


$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'July' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$jul = $row['count'];

$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'August' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$aug = $row['count'];

$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'September' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$sep = $row['count'];


$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'October' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$oct = $row['count'];


$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'November' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$nov = $row['count'];


$result = mysql_query("SELECT count(*) as count from tbl_users where MonthCreated = 'December' and YearCreated = '2016';");
$row = mysql_fetch_assoc($result);
$dec = $row['count'];

You need to retrieve the value of count from each object like I have give in my solution

Aman Maurya
  • 1,305
  • 12
  • 26
  • This is not the efficient way to handle this problem, but as you are new to PHP, so I have solved this problem in simple procedural style so that you could understand. – Aman Maurya Aug 03 '16 at 12:52
0

As @aman said You need to execute query.

You can group Your's results in one query instead of call Db multiple times

SELECT MonthCreated, count(id) AS result 
FROM tbl_users 
WHERE YearCreated = '2016'
GROUP BY MonthCreated

result should be looks like:

[
    'June' => 11
    'July' => 34
    'August' => 56
];

If You want to set those data there 2 ways:

1) if you print resposne js just paste data inline as @chris85 said:

data: [<?php echo $jul, $aug ?>]

but it's ugly solution, allowed only when You learn.

2) Load default js (chart I suppose) and call data by Ajax. Then in PHP You will return Json encoded data. Example thread here

Please use dedicated library like ADODB, to prevent ie. SQL Injection. It will more comforrtable, as You will learn more.

Please don't use camelCase in column names.

Community
  • 1
  • 1
  • Hi Jacek and Aman! Thank you for the reply I tried following as suggested: `$sql3 = mysql_query("SELECT count(*) from tbl_users where MonthCreated = 'August' and YearCreated = '2016';"); $row3 = mysql_fetch_assoc($sql3); $aug = $row3['count'];` I have 2 records in august and tried outputting it but no value appears `data: [1,2,3,4,5,6,7,]` Thank you for your advices and guidance – Jobet Avila Aug 05 '16 at 08:50
  • First thing, check [mysql_query](http://php.net/manual/en/function.mysql-query.php) doc page. You wil see it's deprecated. Use mysql**i** functions (for You it will by only 'i' at the end of mylsq(and rest)). Second, You can check varabile type using `var_dump()` function. `mysqli_fetch_assoc` returns associative array, so you check fields keys using `var_dump()` and print them using ie. `$aug['count(*)']`, or if You will use alias in query `SELECT count(*) AS result`, print `echo $aug['result']` – Jacek Kubicki Aug 07 '16 at 18:18