Charlie
Charlie

Reputation: 11777

MySQL recursive WHERE?

I have a database which looks like this:

Token (PRIMARY)     School        Skip
--------------------------------------
f2342f              School 1      0
434fbc              School 1      0
33332c              School 1      0

My PHP code sends out a Push Notification to each Token if the School is matched up with a temp database. Then it sets skip to 1, so that it doesn't try sending another notification for a day.

The problem is though that I can't have the script set multiple skip values at once. When I do, it seems to work fine for the first token, but then won't set the skip value for the others, and dies.

Here's my PHP:

// Run comparison SQL query
        $compare = mysql_resultTo2DAssocArray(mysql_query("SELECT Temp.School, Temp.Status, Snow.Skip, GROUP_CONCAT(Snow.Token SEPARATOR '\', \'') Tokens FROM Temp JOIN Snow USING (School) WHERE Skip = 0 GROUP BY Temp.School"), $con);
        $amount = count($compare);

        // Send Push Notifications
        for ($i = 0; $i < $amount; $i++) {
            $message = $compare[$i][School] . " - " . $compare[$i][Status];
            $tokens = $compare[$i][Tokens];
            pwCall( 'createMessage', array(
                'application' => PW_APPLICATION,
                'username' => PW_LOGIN,
                'password' => PW_PASSWORD,
                'notifications' => array(
                    array(
                        'send_date' => 'now',
                        'content' => $message,
                        'ios_badges' => 1,
                        'ios_sound' => 'bells.caf',
                        'data' => 'daily',
                        'devices' => array($tokens),
                        )
                    )
                )
            );
            if (!mysql_query("UPDATE Snow SET Skip='1' WHERE Token='$tokens[$i]'", $con)) {

                echo "<pre>";
                print_r(str_replace("'", '', $tokens));
                echo "</pre>";

                die('Error: ' . mysql_error());
            }
        }

With the output being:

f2342f, 434fbc, 33332c Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'f2342f', '434' at line 1

So to me it seems that I need to have it set the skip recursively, unless I'm missing something important with my code.

$compare:

Array
(
    [0] => Array
        (
            [School] => School 1
            [Status] => Closed 
            [Skip] => 0
            [Tokens] => f2342f', '434fbc', '33332c
        )

)

Upvotes: 1

Views: 128

Answers (2)

grossvogel
grossvogel

Reputation: 6782

After you do this:

$tokens = $compare[$i]['Tokens'];

$tokens should be something like this:

f2342f', '434fbc', '33332c

Meaning your query should be changed to

"UPDATE Snow SET Skip='1' WHERE Token IN('$tokens')"

Upvotes: 2

Martin Vrkljan
Martin Vrkljan

Reputation: 879

The problem seems to be in how you treat $tokens in your UPDATE query.

You're trying to access the tokens in it as if it were array, but the error message clearly shows you that $tokens is a string - otherwise print_r would tell you it's an array.

You need to explode the $tokens string before making an update query for each token.

$tokensArray = explode (', ', $tokens);

EDIT:

@minitech gave you a good comment on your question; if the tokens were meant to be used in an IN statement (WHERE token IN ($tokens)) you don't need to explode them into an array.

Upvotes: 1

Related Questions