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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP while($var = $arr) question

    I realized today that I can't be too sure of how that construct works...
    In php, I'm used to doing this...
    while($row = pg_fetch_array($result)){
    // do something
    }

    However... today I did this....
    function getRows($result){
    return pg_fetch_array($result);
    }

    and tried

    while($row = getRows($result)){
    // do something...
    }

    But that never terminated... Why is that? What do I need to do to get that to work and terminate?
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #2
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    I *think* the reason it is not terminating is because you are pulling the same item out of the array over and over. When you do while($item = some_fetch_function($sql_result)) you're iterating through the $sql_results. When you abstract it further by making your own function to do that, you're starting your iteration over each time - never moving to the next result.

    You might be able to fix this with the use of references.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by derzok View Post
    I *think* the reason it is not terminating is because you are pulling the same item out of the array over and over. When you do while($item = some_fetch_function($sql_result)) you're iterating through the $sql_results. When you abstract it further by making your own function to do that, you're starting your iteration over each time - never moving to the next result.

    You might be able to fix this with the use of references.
    Sorry, could you be a bit more specific re the "use of references" part?
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,107
    Thanks
    2
    Thanked 326 Times in 318 Posts
    You can only return variables by reference, not the result of a statement.
    return statement; does not work. You will need to do the following -

    PHP Code:
    $var pg_fetch_array($result);
    return 
    $var
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I haven't tested it, but I think your script hangs because arguments in PHP are passed by value.

    Thus, when you call getResult($result), getResult gets a copy of $result and pg_fetch_array operates on this copy. Any changes it does to the internal structure of $result are not propagated back to the original. So, with the next iteration pg_fetch_array returns the same row again because it gets another copy of the unchanged original.

    Try to change getResult to this
    PHP Code:
    function getRows(&$result){
    return 
    pg_fetch_array($result);

    and see if it helps.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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