简体   繁体   中英

extract values from a returned sql query

I am working in 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

this will then be output and variables used to create a pie chart

Here is my sql query

    $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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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