Mattylfc
Mattylfc

Reputation: 1

Google BigQuery Reading Rows into an array

When I try and read the rows into an array from what was extracted from a table in Google BigQuery it only reads the first row into the array.

I have done it the same way in other code and do not know why this is not reading all the rows in.

<?php
    function top10($chart){
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Google_Service_Bigquery::BIGQUERY);
        $bigquery = new Google_Service_Bigquery($client);
        $projectId = 'projectID';

        $request = new Google_Service_Bigquery_QueryRequest();

        $query = "SELECT First_Name, Last_Name, G FROM [" . $chart. "] ORDER BY G DESC LIMIT 10";

        $request->setQuery($query);

        $response = $bigquery->jobs->query($projectId, $request);
        $rows = $response->getRows();

        $ii = 0;
        $i = 0;
        foreach ($rows as $row)
        {
            foreach ($row['f'] as $field)
            {
                $Info[$i][$ii] = $field['v'];
                $ii++;
            }
            $i++;
        }
    }
?>

Upvotes: 0

Views: 1067

Answers (1)

Pentium10
Pentium10

Reputation: 207828

you need to use something like this:

 $rows = $queryResults->rows();
        foreach ($rows as $row) {
            printf('--- Row %s ---' . PHP_EOL, ++$i);
            foreach ($row as $column => $value) {
                printf('%s: %s' . PHP_EOL, $column, $value);
            }
        }

full example here

Upvotes: 1

Related Questions