1

Today I have looked all over the internet for a good answer. I almost got the answer from this site but that solution didn't work.

Here is what I need to do:

In the database there is a token stored that is going to be used for qr codes. I have already made something to generate the qr code when hardcoded:

                $token_qr = "a86ad6352e939eea67da45b8731c3a8d62dcas1r";
                $url_qr = some url;

                $qr_code = array(
                    "token" => $token_qr, 
                    "url" => $url_qr
                ); // end array
                $qr_code_encoded = json_encode($qr_code, JSON_UNESCAPED_SLASHES);
                $smarty->assign('qr_code_encoded', base64_encode($qr_code_encoded));

The base64 string is put in a url so the qr image can be generated.

Now I need to make it dynamically, the url is always the same but the token is always different. In the model where all the database statements are present I made this:

     Class Webservices {
        public function GetToken($token) {
        $pdo = Database::Get();

        $query = "SELECT `site__webservice`.* FROM `site__webservice` WHERE `token` = :token"; // SQL select statement
        $params = array(":token" => $token); // bind params
        $result = $pdo->Select($query, $params); // run query

        // fetch token
        if($result) {
            $row = PDO::FETCH_ASSOC($result);

            return $row[$token];                
        } else {
            return false;
        }
      }
    }

With this function I try to get the token from the database and store this in the $token_qr variable which stand in the controller. To call this I use this:

   $webservices = new Webservices();

   $token_qr = $webservices->GetToken($token);

The output of this function is now always false. Is there something wrong with my statement or is it in the loop that I created?

Maybe it is something really easy but I can't see the problem and find a solution for it. Thanks in advance for the response!

5
  • ` return $row[$token]` why $token ? i think should be only return $row Commented Sep 22, 2015 at 14:52
  • what framework are you using ? Commented Sep 22, 2015 at 14:53
  • @chris85 PDO::FETCH_ASSOC($result); doesn't make sense. Commented Sep 22, 2015 at 14:59
  • Yea, just looked at that again. token usage is incorrect as well, $row[$token] is going to be $row['valuequeryed'] where as it should be $row['token']. Commented Sep 22, 2015 at 15:00
  • I have changed some things in my codes. It now looks like this: $row = $result->fetchAll(FETCH_ASSOC); return $row['token']; but it is still returning false Commented Sep 22, 2015 at 16:34

1 Answer 1

1

You need fetch the result before return, use fetch() or fetchAll(). Seems Select() works likes pdo execute() so it's return PDOStatment, fetch it to get the results.

if($result) {
   $row = $result->fetchAll(PDO::FETCH_ASSOC);
   return $row; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick reaction. I have tried it but the loop still return false...
@B.Goossen, in select() method, check if execute() return an error, add or die(print_r($statement->errorInfo()));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.