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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 ....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.