0

I'd like to SELECT a field called device_token in the statement below. My main goal is to select the token and store it in a new variable.

    $result = query("SELECT IdPhoto, title, IdUser FROM photos p ORDER BY IdPhoto DESC LIMIT 50") ;

Here is the body of the function and what I've tried:

function stream($IdPhoto=0, $token) {

if ($IdPhoto==0) {

    //Here's where I want to grab the device_token
    $result = query("SELECT device_token, IdPhoto, device_token, title, IdUser FROM photos p ORDER BY IdPhoto DESC LIMIT 50", $token) ;

} else {
    //do the same as above, but just for the photo with the given id
    $result = query("SELECT IdPhoto, title, l.IdUser, username FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d' LIMIT 1", $IdPhoto);

}

if (!$result['error']) {
    // if no error occured, print out the JSON data of the 
    // fetched photo data
    print json_encode($result);
} else {
    //there was an error, print out to the iPhone app
    errorJson('Photo stream is broken');
}
}

Is the ($IdPhoto==0) parameter is getting in the way of the $token variable? If so how can one properly do these things?

  1. Select the device_token

  2. Store it in a new variable called $token

  3. Add the $token parameter to function name

16
  • 1
    what is there on login table? if you run this query directly on the DB it returns what you expect? Commented Mar 10, 2016 at 15:14
  • 1
    Add the two tables' definitions. Commented Mar 10, 2016 at 15:15
  • 1
    I just wanted to see the tables' columns (and their data types, constraints etc.) Commented Mar 10, 2016 at 15:19
  • 1
    is device_token binary? How do you retrieve it in PHP? Commented Mar 10, 2016 at 15:20
  • 1
    How do you try to process device_token field in the php code after running the sql query? The 1st of the 3 attempst is incorrect btw, a comma is missing after device_token field. I do not see any issues with the other 2, though. Hence my question again: how do you process the results of the query? Commented Mar 10, 2016 at 15:22

1 Answer 1

2

Use this way:

$result = query("SELECT photos.device_token, IdPhoto, title, l.IdUser, username FROM photos p JOIN login l ON (l.IdUser = p.IdUser) ORDER BY IdPhoto DESC LIMIT 50");

Probably you have device_token in both tables, and the results is getting the empty device_token from the other table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.