Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    functions within functions

    Hi all,

    i was wondering if this is a valid function to use in a class. I would like the result of the query to be a variable in another function.

    PHP Code:
    function sql_all_items($all_items){
        
    $sql "SELECT * FROM items ORDER BY item_id DESC";
        
    $all_items mysql_query($sql);
        return 
    $all_items;

    So somewhere i would like to just be able to do:

    PHP Code:
     function display ($all_items){
          while (
    $row mysql_fetch_array($all_items)) {
               echo 
    "blah blah blah";
    }


    Hope i was clear with what i am trying to do. I seem to lose the value of $all_items whenever i leave the 1st funtion.

    thanks for any suggestions.
    c.c.
    Last edited by ClubCosmic; 10-31-2006 at 12:16 AM.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You have a typo in the first function. You don't need to set an argument for the first function. The data will be overwritten anyway (if you remove the typo). You could make the argument into a reference in which case you won't need to return anything as the variable given as the argument will be changed.
    Last edited by marek_mar; 10-31-2006 at 06:37 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    function sql_all_items(){
        
    $sql "SELECT * FROM items ORDER BY item_id DESC";
        
    $all_items mysql_query($sql);
       return 
    $this->$all_items;

    Is that what i should be doing?

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Without the "$this->" part it will return a MySQL result resource or false (that's what you should be expecting).
    The second function will work with the result of the first function unless the first function returns false. You should check that.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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