PHP 使用 SQL 获取数组中的数据

PHP getting data in an array with SQL

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

$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');
    ?>    

你应该获取数据。

$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']);

希望对您有所帮助。