user3233623
user3233623

Reputation: 383

PHP SELECT parameter syntax

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

Upvotes: 0

Views: 59

Answers (1)

user2423466
user2423466

Reputation: 21

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.

Upvotes: 2

Related Questions