如何分配变量而不是整数


How to assign variable instead of integer

嗨,我是php新手,正在尝试探索!在代码中插入变量时遇到问题。这是我的变量

 $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';");

我想把它放在这里:

$(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',

这正是我想放变量的地方。我想不通。

    data: [$jul,$aug]
    }]
});
});
$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'];

您需要从每个对象中检索count的值,就像我在解决方案中给出的那样

正如@aman所说,您需要执行查询。

您可以将您的结果分组在一个查询中,而不是多次调用Db

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

结果应该看起来像:

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

如果你想设置这些数据有两种方式:

1) 如果你打印resposne js,只需像@chris85所说的那样内联粘贴数据:

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

但这是一个丑陋的解决方案,只有当你学会了才允许。

2) 加载默认的js(我想是图表)并通过Ajax调用数据。然后在PHP中,您将返回Json编码的数据。此处为示例线程

请使用像ADODB这样的专用库,以防止SQL注入。它会更舒适,因为你会学到更多。

请不要在列名中使用camelCase。