在php中使用mysql fetch结果填充二维数组


Filling a two dimensional array with mysql fetch results in php

我刚刚有一个创建图表的框架,这是它如何正常工作的。

$p = new chartphp();
$p->data = array(array(
array("A",2),
array("B",3),
array("C",23),
array("D",10)   
));
$p->chart_type = "bar";
// Common Options
$p->xlabel = "My X Axis";
$p->ylabel = "My Y Axis";
$out = $p->render('c1');

这种方式工作得很好,现在我需要从sql查询中获得结果并填充数组。

$query ="SELECT t.date AS dates,COUNT(t.id) AS trans FROM Gab AS g, Transaction AS t WHERE t.date BETWEEN '2015-07-30' AND '201-07-10' AND g.TID = '1401009' ORDER BY DATES";
$ask = mysql_query($query) or die("Error");
//Now I try to load the results into the array to be integrated into the API.
$p = new chartphp();
$p->data = array(array(
while($recon = mysql_fetch_array($ask)
{
array($recon['dates'],recon['trans']),
}
));
$p->chart_type = "bar";
// Common Options
$p->xlabel = "My X Axis";
$p->ylabel = "My Y Axis";
$out = $p->render('c1');

我试过这个,但它不工作,数组似乎不加载!

我实际上不确定像你这样嵌套while会做什么,我现在无法实验,但这样的东西应该让你在正确的方向:

$p->data = array(array());
while($recon = mysql_fetch_array($ask))
{
  $p->data[0][] = array($recon['dates'], $recon['trans']);
}

初始化数组,然后在循环中追加元素。