gpt4 book ai didi

PhpMygraph - 如何从数据库 mysql 分配数组

转载 作者:行者123 更新时间:2023-11-30 01:23:53 25 4
gpt4 key购买 nike

这是我的代码:

 <?php    
//Set content-type header
header("Content-type: image/png");

//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
include("koneksi.php");

//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;


//Set data
$query="select col1, col2 from tab1 where sub_project = 'sometext'";
$result=mysql_query($query);
$row = mysql_fetch_assoc($result);
$data = array(
$row['col1'] => $row['col2'],
);

//Create phpMyGraph instance
$graph = new phpMyGraph();

//Parse
$graph->parseVerticalColumnGraph($data, $cfg);
?>

就我而言,col1 中的数据是文本,col2 中的数据是数字。但使用该代码,图表仅显示第一行值。同时数据库中的值不止一个值。

最佳答案

将获取的结果添加到 while 循环中,如下所示。

<?php    
//Set content-type header
header("Content-type: image/png");

//Include phpMyGraph5.0.php
include_once('phpMyGraph5.0.php');
include("koneksi.php");

//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;


//Set data
$query="select col1, col2 from tab1 where sub_project = 'sometext'";
$result=mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[$row['col1']] = $row['col2'];
}

//Create phpMyGraph instance
$graph = new phpMyGraph();

//Parse
$graph->parseVerticalColumnGraph($data, $cfg);
?>

并且$data是多数组,并且具有数据库中的所有记录

希望这段代码对你有帮助

关于PhpMygraph - 如何从数据库 mysql 分配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18226807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com