简体   繁体   English

从返回的SQL查询中提取值

[英]extract values from a returned sql query

I am working in PHP. 我正在使用PHP。 I have a database that i pull data from. 我有一个数据库,我从中提取数据。 I have at the moment the data output to a table. 我现在有数据输出到一个表。

My wish is to take this table and seperate the data to pass to seperate variables, do i some how pass to an array? 我的愿望是使用此表并分离数据以传递给单独的变量,我是否应该如何传递给数组?

eg original 例如原始

Name          hours
Bob             4
Harry           6
Bob             2
Harry           5
Dave            1
Bob             2

Returned and in table 返回并在表中

Bob             3
Harry           2
Dave            1

i'd like to take each value seperately as i wish and also in row order so i then have variable 我想按照我的意愿并按照行顺序分别取每个值,所以我便有了变量

Bob = 3, Harry = 2, Dave = 1

and also take the values of 3,2,1 并取值3,2,1

this will then be output and variables used to create a pie chart 然后将其输出,并将变量用于创建饼图

Here is my sql query 这是我的SQL查询

    $dbHandle = new PDO("mysql:host=$host;dbname=$dbName",$user,$pass);
    $query ="SELECT platform, count(*) FROM review GROUP BY platform";
    $sqlQuery = $query; // put your students table name
    echo $sqlQuery;  //helpful for debugging to see what SQL query has been created
    $statement = $dbHandle->prepare($sqlQuery); // prepare PDO statement
    $statement->execute();   // execute the PDO statement
    echo "<table border='1'>";
    while ($row = $statement->fetch()) {
    echo "<tr><td>" . $row[0] ."</td><td>". $row[1];}
    echo "</table>";
    $dbHandle = null;

You can name SQL columns in the query with AS and order it with ORDER BY : SELECT platform AS plat, count(platform) AS count FROM review GROUP BY platform ORDER BY count DESC 您可以使用AS命名查询中的SQL列,并使用ORDER BY对其进行排序: SELECT platform AS plat, count(platform) AS count FROM review GROUP BY platform ORDER BY count DESC

In the code where you fetch the results you can choose to fetch each row as an associative array: 在获取结果的代码中,您可以选择以关联数组的形式获取每一行:

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {//this will fetch results and index them into the row array with column names:
    echo "<tr><td>" . $row['plat'] ."</td><td>". $row['count']."</td></tr>";
}

This might be the easy way out, but keep in mind that you might want to order them differently later on. 这可能是简单的方法,但是请记住,以后可能要对它们进行不同的排序。 Also echoing out the results as you get them might be a bad design. 在获得结果时也回显结果可能是一个错误的设计。 A better way would be to store them in an array and later loop and echo it out. 更好的方法是将它们存储在数组中,然后循环并回显它。 So here it is: 所以这里是:

$results = $statement->fetchAll(PDO::FETCH_ASSOC);

//order the results the way you want here

echo "<table border='1'>";
//then echo them out:
foreach($results as $key => $result){
    echo "<tr><td>" . $result['plat'] ."</td><td>". $result['count']."</td></tr>";
}
echo "</table>";

All in all here's the example with your code: 总而言之,这是您的代码示例:

$dbHandle = new PDO("mysql:host=$host;dbname=$dbName",$user,$pass);
$query ="SELECT platform AS plat, count(platform) AS count FROM review GROUP BY platform ORDER BY count DESC";
$sqlQuery = $query; // put your students table name
//echo $sqlQuery;  //helpful for debugging to see what SQL query has been created
$statement = $dbHandle->prepare($sqlQuery); // prepare PDO statement
$statement->execute();   // execute the PDO statement
$results = $statement->fetchAll(PDO::FETCH_ASSOC);//data is here, just draw the chart

//you can access the data in the array like this: 

foreach($results as $key => $value){//$value is one row of results
    echo '<some html you="need" for the piechart>'.$value['plat']/*name of the platform*/.'</some closing tag perhaps><another tag>'.$value['count'].'</closed>';
}
$dbHandle = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM