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

时间:2021-02-17 21:38:31

I'm working on a graph for my website. I want to show different data in this graph. I have a mysql database with a table with four rows. I want to save these four rows in an array, this array will be visible on the graph.

我正在为我的网站制作图表。我想在此图中显示不同的数据。我有一个带有四行的表的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);

This are the rows in my database. i'm saving the value of them in a variable.

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

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

And this will be my array.

这将是我的阵容。

Unfortunately, this does not give the desired result. because all values are equal in the graphics, I think this is because he does not get the right data. I guess I did something wrong in my code, and i hope someone can help me with this problem.

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

Below you will find the whole graph code, well i wont copy the included page..

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

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

1 个解决方案

#1


0  

You should fetch datas.

你应该获取数据。

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

I hope it helps.

我希望它有所帮助。

#1


0  

You should fetch datas.

你应该获取数据。

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

I hope it helps.

我希望它有所帮助。