1

I'm having trouble with a PHP function for a very small WYSIWIG-like app. The following function determines whether a user owns a page. It has print statement for debugging.

function is_owned($sitename, $page_id)
{
    $output = false;

    $result = count(single_row_query(sprintf("SELECT page_id FROM pages WHERE sitename='%s' AND page_id='%s';", addslashes($sitename), addslashes(strval($page_id)))));

    print $result;

    if ($result != 0)
        {$output = true; }

    return $output;
}

The following is the output of the page I'm using to debug the function.

SELECT sitename FROM session WHERE uid='l8rc8ssri8qr65lqvtfp174s91'
SELECT page_id FROM pages WHERE sitename='Test' AND page_id='132432';
Result: 0
The function has returned true.

As you can see, the value of $result is 0, but the function still returned true. The SQL statements are included, though the problem doesn't seem to lie there.

The following function is called be the is_owned(), but is not likely to be relevant, as it seems to be functional.

function single_row_query($query)
{
    $database_connection = open_connection();

    $result = mysql_query($query);

    $output = mysql_fetch_array($result);

    $output = $output[0];

    mysql_free_result($result);
    mysql_close($database_connection);

    return $output;
}

Thank you very much for your assistance.

Edit: Additionally, this is the output of the test page when it is supposed to return true:

! SELECT sitename FROM session WHERE uid='l8rc8ssri8qr65lqvtfp174s91' !
! SELECT page_id FROM pages WHERE sitename='Test' AND page_id='7'; !
Result: 1
The function has returned true.
2
  • 2
    Could you include the code that prints 'The function has returned true'? Maybe that's the problem. Commented Mar 2, 2012 at 7:14
  • 1
    Oh, derp. You're completely correct, I forgot to put a $ on a variable. Sorry for the trivial question. Commented Mar 2, 2012 at 7:20

4 Answers 4

2

Your problem was the result of a typo, but here is why the other answers are completely wrong.

PHP count always returns an int. Specifically:

Returns the number of elements in var. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.

Regardless of what else happens in the single_row_query function, count will return an int. At that point, there is no practical difference between $result != 0 and $result !== 0 or the other posited issues. Strong type comparison doesn't matter because you will necessarily be comparing an int value with an int value.

At that point, your function was sound and your problem must reside somewhere else.

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

Comments

-1

Try:

if ($result !== 0) {
    $output = true;
}

1 Comment

It is still returning true when $result is 0.
-1

Try this condition [UPDATED]

if(!empty($result))
{$output = true; }

1 Comment

The function doesn't have access to the SQL result, its handled by another function. The $result variable is the first (and only) row of the SQL result.
-2
  1. make var_dump($result) instead of print $result in cause of var_dump prints type of variable (print converts variable to string), so you will see real value.
  2. use if ($result !== 0) for strong type comparison.

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.