嗨我是新手到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]
}]
});
});
答案 0 :(得分: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'];
您需要从我在解决方案中提供的每个对象中检索count的值
答案 1 :(得分:0)
正如@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编码数据。示例线程here
请使用像ADODB这样的专用库来防止ie。 SQL注入。 它将更加可信,因为您将了解更多信息。
请不要在列名中使用camelCase。