Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 来自mySQL的JSON和Google图表中的着色_Php_Mysql_Google Visualization - Fatal编程技术网

Php 来自mySQL的JSON和Google图表中的着色

Php 来自mySQL的JSON和Google图表中的着色,php,mysql,google-visualization,Php,Mysql,Google Visualization,我想得到一些帮助,用谷歌图表给数据着色。我试图比较两个具有日期和排名值的域 如果我省略了“域”数据,只包含日期和值数据,它将显示一个图表,其中日期数据在y轴上,值数据在x轴上。我想使用表中的域数据来区分我正在比较的数据,一种颜色的域与条形图中另一种颜色的域,条形图中的图例标识了域 基本上使用下面的代码,我得到以下错误: 给定轴上的所有系列必须具有相同的数据类型 如果有不同或更简单的方法,请告诉我 表结构: 列类型空默认注释 日期文本编号 值文本编号 域文本号 下面是我用来将mysql数据更改为谷

我想得到一些帮助,用谷歌图表给数据着色。我试图比较两个具有日期和排名值的域

如果我省略了“域”数据,只包含日期和值数据,它将显示一个图表,其中日期数据在y轴上,值数据在x轴上。我想使用表中的域数据来区分我正在比较的数据,一种颜色的域与条形图中另一种颜色的域,条形图中的图例标识了域

基本上使用下面的代码,我得到以下错误: 给定轴上的所有系列必须具有相同的数据类型

如果有不同或更简单的方法,请告诉我

表结构:

列类型空默认注释

日期文本编号
值文本编号
域文本号

下面是我用来将mysql数据更改为谷歌友好的JSON的方法:

<?php
/* $server = the IP address or network name of the server
 * $userName = the user to log into the database with
 * $password = the database account password
 * $databaseName = the name of the database to pull data from
 */
$host="127.0.0.1"; //replace with your hostname
$username="XXXXXXX"; //replace with your username
$password="XXX"; //replace with your password
$db_name="KYW_data"; //replace with your database
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name") or die ("cannot select DB");

mysql_select_db($db_name, $con); 

// write your SQL query here (you may use parameters from $_GET or $_POST if you need them)
$query = mysql_query('SELECT date, value FROM KYW_Compete_rank');

$table = array();
$table['cols'] = array(
/* define your DataTable columns here
* each column gets its own array
* syntax of the arrays is:
* label => column label
* type => data type of column (string, number, date, datetime, boolean)
*/
    array('label' => 'date', 'type' => 'string'),
array('label' => 'value', 'type' => 'number'),
array('label' => 'value', 'type' => 'string')
// etc...
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {
    $temp = array();
// each column needs to have data inserted via the $temp array
$temp[] = array('v' => $r['date']);
$temp[] = array('v' => $r['value']);
$temp[] = array('v' => $r['domain']);
// etc...
// insert the temp array into $rows
    $rows[] = array('c' => $temp);
}

要使图例显示两个不同的系列,必须将数据分成两列。您可以使用如下SQL拆分数据:

SELECT
    date,
    SUM(IF(<condition for series 1>, value, 0)) AS value_1,
    SUM(IF(<condition for series 2>, value, 0)) AS value_2
FROM KYW_Compete_rank
GROUP BY date

可能是因为我不知道如何设置条件,也许这就是为什么它不起作用。在我的表中,我有一个domain列,那么条件是domain=='犹他州edu',domain='犹他州edu'还是其他一些用IF语句捕获不同域的条件呢?我尝试了以下终端形式:
mysql-u root-p-e'选择日期,SUM(IF(domain='byu.edu',value,0))作为值_1,SUM(IF(domain='犹他州edu',value,0))作为KYW_Competite_rank GROUP BY date'KYW_data
的值_2,我在第1行得到了“错误1054(42S22):“字段列表”中的未知列'byu.edu'”我不知道为什么会出现该错误,因为您没有选择列
byu.edu
SELECT
    date,
    SUM(IF(<condition for series 1>, value, 0)) AS value_1,
    SUM(IF(<condition for series 2>, value, 0)) AS value_2
FROM KYW_Compete_rank
GROUP BY date
$table = array(
    'cols' => array(
        array('label' => 'date', 'type' => 'string'),
        array('label' => 'value 1', 'type' => 'number'),
        array('label' => 'value 2', 'type' => 'number')
    )
    'rows' => array()
);
while($r = mysql_fetch_assoc($query)) {
    $data['rows'][] = array('c' => array(
        array('v' => $r['date']),
        array('v' => $r['value_1']),
        array('v' => $r['value_2'])
    ));
}
echo json_encode($data, JSON_NUMERIC_CHECK);