2016-08-03 39 views
0

嗨即时通讯新的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', 

这里到底是哪里IM tyring把我的变量。不能解决这个问题。

data: [$jul,$aug] 

    }] 
}); 
}); 
+0

您需要获取结果对象。另外'$ jul'不是PHP,除非你输入PHP块并输出它'<?php echo $ jul; ?>'。 – chris85

回答

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']; 

你需要从每个对象中检索计数的值就像我在我的解决方案

+0

这不是解决这个问题的有效方法,但是由于您是PHP的新手,所以我已经用简单的过程样式解决了这个问题,以便您能够理解。 –

0

已经给作为@aman说你需要执行查询。

你可以将你的成绩在一个查询,而不是调用Db的多次

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

结果应该是看起来像:

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

如果您要设置这些数据有2种方式:

1)如果您打印resposne js只需粘贴数据为@ chris85说:

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

但它的丑陋的解决方案,只有当你学习允许。

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

请使用像ADODB这样的专用库来防止ie。 SQL注入。 它会更具可操作性,因为您会了解更多信息。

请不要在列名中使用camelCase。

+0

嗨Jacek和阿曼!谢谢你的回复,我试着按照建议: '$ sql3 = mysql_query(“SELECT count(*)from tbl_users where MonthCreated ='August'and YearCreated ='2016';”); $ row3 = mysql_fetch_assoc($ sql3); $ aug = $ row3 ['count'];' 我在8月有2条记录,并尝试输出它,但没有出现数值 'data:[1,2,3,4,5,6,7, ?php echo $ aug?>]' 感谢您的建议和指导 –

+0

首先,请查看[mysql_query](http://php.net/manual/en/function.mysql-query.php)doc页面。你会看到它已被弃用。使用mysql ** i **函数(对于你而言,只有'i'在mylsq(和rest)的末尾)。其次,您可以使用'var_dump()'函数检查varabile类型。 'mysqli_fetch_assoc'返回关联数组,所以你使用'var_dump()'检查字段键并使用ie打印它们。 '$ aug ['count(*)']',或者如果您在查询'SELECT count(*)AS result'中使用别名,则输出'echo $ aug ['result']' –