0

This is weird ... obviously I am missing something very obvious, but here goes got the sell for done and it feeds in with the symbol to sell. Need to determine if the stock is owned. Easy, right..

// query holdings for stock
$position = CS50::query("SELECT shares FROM holdings WHERE user_id = ? AND symbol = ?",$_SESSION["user"], $_POST["symbol"]);
if ($position == false);
{
    dump($position);
    apologize("You do not own that stock.");
}

Well, it dumps:

Array
(
    [0] => Array
        (
            [shares] => 222
        )
)

So, obviously NOT empty....BTW a non- owned stock gives:

Array
()

I've spent a day on this..... suggestions? Oh, and using === in the if statement provides the same result.

Eternal gratitude for the one who solves this...

3 Answers 3

1

You need to go back to the pset specs to determine the return value of SELECT, obviously it's not boolean in this case.

1

Actually you have 2 mistakes.

  1. As @ronga correctly suggested, query() doesn't return a bool, so you can't check for $position == false, simply because $position is an array and not a bool.
  2. The other mistake and the reason you always get the output, is that you have ended your if statement with a semicolon:

    if ($position == false);
    

    which makes the interpreter always enter the curly braces, no matter the value of $position.

You correctly figured out though that what you needed was to check if the array was empty using empty(). I guess you did something like:

if (empty($position))

and you also removed the semicolon in the process.

-2

Thanks for a non-answer ronga.

The solution is to use the function empty(). Did that and it works.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .