Jump to content

PDO returning single string not associative array.


hackalive

Recommended Posts

Hi guys,

I am using this oAuth library https://github.com/elbunce/oauth2-php

But more specifically these lines of code:

protected function getToken($token, $isRefresh = true) {
	try {
		$tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS;
		$tokenName = $isRefresh ? 'refresh_token' : 'oauth_token';

		$sql = "SELECT $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE token = :token";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':token', $token, PDO::PARAM_STR);
		$stmt->execute();

		$result = $stmt->fetch(PDO::FETCH_ASSOC);

		return $result !== FALSE ? $result : NULL;
	} catch (PDOException $e) {
		$this->handleException($e);
	}
}

 

However, this is retuning no value.

If I modify the code to be:

protected function getToken($token, $isRefresh = true) {
	return $token;
}

It returns the $token value, so the $token is definitely being passed to the function.

 

The code should return an associative array: i.e., $token["expires"], $token["client_id"], $token["userd_id"], $token["scope"], etc

Also $token should not === NULL.

 

PS. I run a few checks on

		$tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS;
		$tokenName = $isRefresh ? 'refresh_token' : 'oauth_token';

And they are returning the correct values.

 

Which from my thinking narrows it down to:

		$sql = "SELECT $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE token = :token";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':token', $token, PDO::PARAM_STR);
		$stmt->execute();

		$result = $stmt->fetch(PDO::FETCH_ASSOC);

		return $result !== FALSE ? $result : NULL;

 

Any and all help is greatly appreciated.

 

Thanks in advance.

So you guys know what I have tried and maybe can help narrow down the prolem.

 

If I do this (add 'return' to the line in its exiting place):

return $stmt->bindParam(':token', $token, PDO::PARAM_STR);

It reruns the a value of:

1

 

 

However if I was to return the execute() line I again get no value returned.

Your code looks ok to me as it is.  Your query is probably just returning zero-rows so your ->fetch call is returning false.  Run the query manually in mysql to verify (substituting the proper value for your :token placeholder).

Ran the Manual SQL query.

It found a result.

 

I updated the code to run the SQL query to:

[php
$sql = "SELECT $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE token = :token";
[/code]

If I echo the return it now gives me:
Array

 

However if I echo $token["expires"] I get:

Notice: Undefined index: expires in ....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.