【问题标题】:PHP getting data in an array with SQLPHP使用SQL获取数组中的数据
【发布时间】:2015-01-08 11:17:05
【问题描述】:

我正在为我的网站制作图表。我想在此图中显示不同的数据。 我有一个带有四行表的 mysql 数据库。 我想将这四行保存在一个数组中,这个数组将在图表上可见。

$transportertraindb = mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'trein'", $db);
$transporterlorrydb = mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'vrachtauto'", $db);
$transporterinlanddb = mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'binnenschip'", $db);
$transporterseadb = mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'zeeschip'", $db);

这是我数据库中的行。我将它们的值保存在一个变量中。

$data = array('Zeeschip'=>$transporterseadb, 'Binnenvaart'=>$transporterinlanddb, 'Trein'=>$transportertraindb, 'Vrachtwagen'=>$transporterlorrydb); 

这将是我的数组。

不幸的是,这并没有给出预期的结果。因为图形中的所有值都是相等的,我认为这是因为他没有得到正确的数据。我想我的代码做错了,希望有人能帮我解决这个问题。

你会在下面找到整个图形代码,我不会复制包含的页面..

 <?php
include('graidlechart/graidle.php');
    / array with number of tourists, by countries
    $data = array('Zeeschip'=>$transporterseadb, 'Binnenvaart'=>$transporterinlanddb, 'Trein'=>$transportertraindb, 'Vrachtwagen'=>$transporterlorrydb);        


    // set 2 numeric arrays, one with countries (for legend), another with tourists number
    $cnt = array_keys($data);
    $tor = array_values($data);

    // create object of graidle class (define Title)
    $graph = new graidle('Representation containers of transporters');
    $graph->setColor('#a7b8ed');
    $graph -> setValue($tor,'p');      // set pie chart (p=pie)

    $graph -> setLegend($cnt);             // to add a legend
    $graph -> setExtLegend(1);             // to display percentage, and numbers
    $graph -> setWidth(420);               // graphic chart width

    $graph -> create();                       // create chart
    $graph->carry2file('charts/', 'graphic_chart_3');
    ?>    

【问题讨论】:

  • 您在哪里从数据库中获取结果集?在分配数组值之前,您是否在任何地方使用mysql_fetch_arraymysql_fetch_assoc?而mysql_* 是易受攻击的代码。
  • 你应该使用mysql_fetch_array
  • @BeatAlex 编号:mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )
  • @lolka_bolka 对不起,你是对的!
  • 没问题,只是不想混淆OP。

标签: php mysql arrays database web


【解决方案1】:

你应该获取数据。

$transportertraindb = mysql_fetch_array(mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'trein'", $db));
$transporterlorrydb = mysql_fetch_array(mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'vrachtauto'", $db));
$transporterinlanddb = mysql_fetch_array(mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'binnenschip'", $db));  
$transporterseadb = mysql_fetch_array(mysql_query("SELECT container_counter FROM transporter WHERE transporter_name = 'zeeschip'", $db));
$data = array('Zeeschip'=>$transporterseadb['container_counter'], 'Binnenvaart'=>$transporterinlanddb['container_counter'], 'Trein'=>$transportertraindb['container_counter'], 'Vrachtwagen'=>$transporterlorrydb['container_counter']);

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 2019-08-21
    • 2016-10-07
    • 1970-01-01
    • 2022-01-10
    • 2015-03-25
    • 2016-12-28
    • 2013-12-30
    相关资源
    最近更新 更多