WEB Returning from a function

Discussion in 'OT Technology' started by hurleyint1386, Sep 13, 2009.

  1. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So I used to always write my code spaghetti style and started using functions to organize things a little bit easier. The only problem I've come into is if I want to use a function to connect to a database then select some data, how do I return that information to the original page? It's probably something simple that I'm missing. Halp?
     
  2. hsmith

    hsmith OT Supporter

    Joined:
    Feb 24, 2002
    Messages:
    124,630
    Likes Received:
    727
    Location:
    Your mother.
    what language would probably help
     
  3. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    sorry, php
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    if you are using mysql.. php and mysql are tightly coupled..

    simple commands like

    $result = mysql_query("query"); <-- to query the database
    $row = mysql_fetch_row($result); <-- pulls the data
    $rows = mysql_fetch_assoc($result) <-- pulls data and makes it an assoc array and you can access the data by column names

    echo $row[0] to access your return data

    or for multiple rows

    while($rows = mysql_fetch_assoc($result)){
    echo $rows['id']; //echos out the id column
    }


    hope that helps
     
  5. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    sorry, that's not what I'm looking for. I have a page that calls function getInfo(); then getInfo performs a function, then I want to return the results of that function to the page.
     
  6. Insert Tokens

    Insert Tokens Making Cancer My Bitch OT Supporter

    Joined:
    Jan 12, 2006
    Messages:
    8,330
    Likes Received:
    76
    Location:
    Tasmania
    The function should have a return results line in it.. then you just call the function where you need it, or place it into a variable and use it :dunno:

    Eg (from: http://www.w3schools.com/PHP/php_functions.asp):

    Code:
    <?php
    function add($x,$y)
    {
    $total=$x+$y;
    return $total;
    }
    
    echo "1 + 16 = " . add(1,16);
    ?>
    
    Or at the end just do:

    Code:
    $finalresults = add(1,16);
    
    And use $finalresults wherever you want..



    Or am I misunderstanding :dunno:
     
  7. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    That's exactly what I'm looking for, now how about if I'm returning an array? Would I do $finalresults[] = getInfo(); ?
     
  8. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    oh........................

    yeh just make sure you return an array type
     
  9. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Son of a bitch. No wonder why it wouldn't work. I didn't give my inputs a name! Fuuuuuu
     
  10. Insert Tokens

    Insert Tokens Making Cancer My Bitch OT Supporter

    Joined:
    Jan 12, 2006
    Messages:
    8,330
    Likes Received:
    76
    Location:
    Tasmania
    Usually helps ;)
     

Share This Page