SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regarding Returning Variables & Negation

    Hey guys,

    I have a few questions that I should already know the answer to:

    IF a variable is declared as FALSE, then does that mean running an if(!$variable) {} will run the functions in the { } 's ?

    Also, when using classes, you can simply return variables to the script using return $variable, correct?

    The reason I ask, I have a function that I have gotten to work in other scripts with a very similar set up, but not in this new setup:

    Function:
    PHP Code:
        function id_query($table,$id) {
        
    $query "SELECT * FROM $table WHERE id = $user_id";
        
    $result mysql_query($query);
        return 
    $result;
            if(!
    $result) {
                
    $non_existant TRUE;
                return 
    $non_existant;
            } else {
            
    $information mysql_fetch_array($result);
            return 
    $information;
            }
        } 
    Script:

    PHP Code:
    $database->id_query('student_information'$user_id);
        if (
    $non_existant == TRUE) {
            echo 
    "False";
            
    $student_info FALSE;
        } else {
            
    $student_info TRUE;
            echo 
    "True";
            
    $student_array $information;
        } 
    It always returns TRUE ( as in there is information in the table where id = $user_id) , and attempts to update the table, even though there is no information in the database.
    ]

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Unless you wrote a query that actually fails and produces an error, mysql_query() isn't going to return false. Your "return $result;" will be true no matter how many rows are returned.

    Check for zero rows using mysql_num_rows()

    Edit:

    I shouldn't say "will be true"... it'll return a resource, which is non-FALSE, whether there are rows in the result set or not

  3. #3
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So although $result fails, it still returns a non-FALSE result, so using !$result in my if statement is a stupid idea to begin with.

    I usually use mysql_num_rows, but I thought I could cut down on the extra line of code and use $result, I'll give num_rows a shot again and see how it goes.
    ]

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MilchstrabeStern
    So although $result fails
    That's the logic mistake. It doesn't fail if the query executes. Returning zero rows is not the same as a query failing.

  5. #5
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Revised function:

    PHP Code:
        function id_query($table,$id) {
        
    $query "SELECT * FROM $table WHERE id = $user_id";
        
    $result mysql_query($query);
        
    $num mysql_num_rows($result);
        
            if(
    $num == 0) {
                
    $non_existant TRUE;
                return 
    $non_existant;
            } else {
            
    $information mysql_fetch_array($result);
            return 
    $information;
            }
        } 
    Error that returns:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mplionhe/public_html/registration/forms/database.php on line 26

    I think this might have been the reason I didn't use mysql_num_rows from the start, unless I am doing something wrong.
    ]

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your function takes $id as a parameter, but you use $user_id in the query.

    You can still do error checking on mysql_query() to catch real errors like this -- it won't go off if your query returns zero rows, only if you send something that's not SQL like you seem to be doing here.

    PHP Code:
    mysql_query($query) or die(mysql_error()); 

  7. #7
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent catch.

    Revised Function:

    PHP Code:
        function id_query($table,$id) {
        
    $query "SELECT * FROM $table WHERE id = $id";
        
    $result mysql_query($query);
        
    $num mysql_num_rows($result);
        
        
    $non_existant FALSE;
        
            if(
    $num == 0) {
                
    $non_existant TRUE;
                return 
    $non_existant;
            } else {
                
    $information mysql_fetch_array($result);
                return 
    $information;
            }
        } 
    It works now, but is still returning true for the following code, even though there is no information in the table:

    PHP Code:
    $database->id_query('student_information'$user_id);
        if (
    $non_existant == TRUE) {
            echo 
    "False";
            
    $student_info FALSE;
        } else {
            
    $student_info TRUE;
            echo 
    "True";
            
    $student_array $information;
        } 
    ]

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You're not setting $non_existant to the result of the function call, and $non_existant in the function is a local variable to that function.

    PHP Code:
    $non_existant $database->id_query('student_information'$user_id); 

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see what you're trying to do... populate two variables in the function to use outside of it...

    http://us3.php.net/global

    Those variables are local to the function and stop existing once the function ends. They don't refer to the same variable even if it exists by the same name outside the function. You can force that using the global keyword, but it seems like a bad idea.

  10. #10
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See, thats where I am confused Dan.

    I always thought using return $variable, allowed the script to recieve the variable from a function within a class.

    You actually have to set the variable equal to the function, and then when it is defined within the function, it is defined to the $variable within the script?

    It works now, thanks!

    Edit:


    Actually, it always returns FALSE now...

    BTW, did you use ajax for the dynamic graph on your Analytics page? It's very impressive!
    ]

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I always thought using return $variable, allowed the script to recieve the variable from a function within a class.

    You actually have to set the variable equal to the function, and then when it is defined within the function, it is defined to the $variable within the script?
    Yes, this is how functions work in most languages. They have a return value, which can be used as the right-hand operator for assignment to something else. It need not be a variable by the same name. Returning something simply returns a value or reference, it does not 'do' anything with that variable, like placing it into global scope.

    You can force those variables into the global scope so they're accessible outside the function with the global keyword.

    My recommendation would be something like this...

    PHP Code:
    function id_query($table,$id) {
        
    $query "SELECT * FROM $table WHERE id = $id LIMIT 1";
        
    $result mysql_query($query) or die(mysql_error());
        
        if (
    mysql_num_rows($result) > 0) {
            return 
    mysql_fetch_array($result);
        } else {
            return 
    NULL;
        }
    }

    $result $database->id_query('student_information'$user_id);
    if (
    $result != NULL) {
        
    $student_info TRUE;
        
    $student_array $result;
    } else {
        
    $student_info FALSE;
        echo 
    "False";

    Off Topic:

    The graphs on v3 are Flash and take XML data for input. I chose that to cut down on CPU cycles being used generating graphs on the fly so that the server can handle tracking for more websites instead.

  12. #12
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really need to get a firmer grasp of my OOP, but, when you use:

    PHP Code:
     return mysql_fetch_array($result); 
    How does the script know that $result = mysql_fetch_array($result) and not $result = mysql_query($query) or die(mysql_error()); ?
    ]

  13. #13
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    you are getting the value returned from mysql_fetch_array() and returning that value not the function itself.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  14. #14
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are getting the value returned from mysql_fetch_array() and returning that value not the function itself.
    I understand that.

    What I was missing was the NULL that is returned on the else :

    PHP Code:
        if (mysql_num_rows($result) > 0) {
            return 
    mysql_fetch_array($result);
        } else {
            return 
    NULL;
        } 
    I get it now, thanks!
    ]

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IF a variable is declared as FALSE, then does that mean running an if(!$variable) {} will run the functions in the { } 's ?
    That would produce:
    Code:
    if(!FALSE)
    {
      ....
    }
    and since !FALSE is TRUE, that would evaluate to:
    Code:
    if(TRUE)
    {
    
    }

  16. #16
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can simply return variables to the script using return $variable, correct?
    A simple return statement just returns a value:
    Code:
    return 10;
    However, you can also have 'expressions' in your return statement:
    Code:
    return $num;
    $num is first evaluated and then the return statement executes. If $num = 10, then that statement produces;
    Code:
    return 10;
    You can also have more complicated expressions in a return statement:
    Code:
    return someFunc() + 3;
    A function call is replaced by its return value, so if someFunc() is defined like this:
    Code:
    function someFunc()
    {
    	return 10;
    }
    then in this statement:
    Code:
    return someFunc() + 3;
    the function call is replaced by 10, producing:
    Code:
    return 10 + 3;
    which evaluates to:
    Code:
    return 13;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •