doumouyi4039 2012-06-22 17:25
浏览 73
已采纳

PHP和Mysql如何从一个字段返回多个值

Please excuse my newbie Question.

I'm tryin to display data from a mysql table into a chart (phpChart http://phpchart.net) What I need is to display all values of Field cost for Region 'CA'

E.g

ID  Cost  Region
----------
1   500    CA
----------
2   100    DP
----------
3   280    CA
----------
4   40     ST
----------
5   80     CA
----------

<?php
include 'config.php';
$query = "SELECT Cost From tblForecast where Region='CA' =";
$rs = mysql_query($query);

if (!$rs) {
    echo "Could not execute query: $query";
    trigger_error(mysql_error(), E_USER_ERROR); 
} else {
    echo "Query: $query executed
";
} 

$nrows = mysql_num_rows($rs);

for ($i = 0; $i < $nrows; $i++) {
    $row = mysql_fetch_row($rs);
    echo $row[0];
    echo "<br/>";
}

$pc = new C_PhpChartX(array(array($row[0])),'basic_chart');
\\"This where i'm stuck as to how display the other values within the array"

$pc->set_title(array('text'=>'Basic Chart with Bar Renderer'));
$pc->set_series_default(array('renderer'=>'plugin::BarRenderer'));

$pc->draw();
mysql_close();

?>

I'm able to display the first rows value in the chart only.

Anyone's help would be much appreciated Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongzichan2886 2012-06-22 17:29
    关注

    You're continually fetching your query results into a single variable, and overwriting that data on each iteration of the loop.

    Try this:

    $data = array();
    while ($row = mysql_fetch_array($rs)) {
       $data[] = $row[0];
    }
    
    $pc = new C_PhpChartX($data, 'basic_chart');
    

    Plus, though this is most likely on a cut/paste type, you've got a syntax error in your query anyways:

    $query = "SELECT Cost From tblForecast where Region='CA' =";
                                                             ^---syntax error
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据