0

I have the following code:

$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
    while($row = mysql_fetch_array($mostRecent))
    {
        $mostRecentArr = $row;
    }
    var_dump($mostRecentArr);

The same SQL query in the command line returns 3 results, but this code only returns one result, even though I put LIMIT 3. Any help?

0

3 Answers 3

4
while($row = mysql_fetch_array($mostRecent))
    {
        $mostRecentArr[] = $row['couponID'];
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You reassign the value in the loop each time, then dump the last value. Put var_dump in the loop after the assignment. Like so:

$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
    while($row = mysql_fetch_array($mostRecent))
    {
        $mostRecentArr = $row;
        var_dump($mostRecentArr);
    }

Comments

1

I guess what are you trying to do is:

$mostRecent = mysql_query("SELECT couponID FROM users_coupons WHERE userID = '$userID' ORDER BY id LIMIT 3");
$mostRecentArr = array();
while($row = mysql_fetch_array($mostRecent))
{
    $mostRecentArr[] = $row;
}
var_dump($mostRecentArr);

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.