简体   繁体   中英

SQL BigQuery: fatal error when using PHP string variable in query

I am puzzled by this error:

"invalidQuery", "message": "Field 'QLD' not found in table 'medicare.medicareTable'.", "locationType": "other", "location": "query" } ], "code": 400

because the field QLD is most definitely in that table.

I am trying to get a response from BigQuery using the following query in a php file:

$qld = 'QLD';

$request = new Google_Service_Bigquery_QueryRequest();

$request->setQuery("SELECT Gender, SUM(Cost) AS Cost
FROM [medicare.medicareTable]
WHERE State = $qld
GROUP BY Gender");

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

I have played around with the query and found that if I replace $qld with a string, eg:

WHERE State = 'QLD'

Then it works. I am at a loss. What is going wrong here?

Here's the full error, in case it's needed:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "invalidQuery", "message": "Field 'QLD' not found in table 'medicare.medicareTable'.", "locationType": "other", "location": "query" } ], "code": 400, "message": "Field 'QLD' not found in table 'medicare.medicareTable'." } } ' in /base/data/home/apps/s~s3449107-assign2/1.396261573977805792/php/google-api-php-client/src/Google/Http/REST.php:118 Stack trace: #0 /base/data/home/apps/s~s3449107-assign2/1.396261573977805792/php/google-api-php-client/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\\Psr7\\Response), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...') #2 /base/data/home/apps/s~s3449107-assign2/1.396261573977805792/php/google-api-php-client/src/Google/Task/Runner.php(181): call_user_ in /base/data/home/apps/s~s3449107-assign2/1.396261573977805792/php/google-api-php-client/src/Google/Http/REST.php on line 118

Not

WHERE State = $qld

But

WHERE State = '$qld'

Text is text ;)

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