Re: [PHP-DB] Using a loop on a result destroys array??
On Wednesday 24 November 2004 20:31, Mark Benson wrote: From the above code, Actually the above code would result in a parse error. There are no where-loops in PHP but there are while-loops. This is not being pedantic. If you're going to post code, make sure it's verbatim by using copy and paste your actual code. You want people to focus on your real problem and not on the mistakes you made in transcribing your actual code into your post. loop 2 returns a blank array, the output is simply Array (). If I remove the loop (by commentiung it out) that extracts $crab the mysql $result array is suddenly full of the correct values. It seems to me that loop 1 is destroying or somehow not resetting $result. Very odd... mysql_data_seek() is what you need. Or you can consider storing the results into an array the first time round then you can read results from the array whenever next you need it. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-db -- /* Business is a good game -- lots of competition and minimum of rules. You keep score with money. -- Nolan Bushnell, founder of Atari */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Using a loop on a result destroys array??
The real problem you're having is the statement says while there are results in this query, grab them - once you reach the end of the first while loop, there are no more results, so the second one is skipped right over - it's condition *never* evaluates true. If the query isn't changing, and there aren't other conditions/etc. - then just grab all the data the first time through and put it where it needs to go. $array_stuff = array(); $line = 0; while (mysql_fetch_array($result) as $row1) { // data for first set of operation(s) $crab = $row1[bar]; // data for second set of operation(s) $array_stuff[$line] = array() $array_stuff[$line][foo] = $row1[foo]; $array_stuff[$line][bar] = print $row1[bar]; $array_stuff[$line][steak] = print $row1[steak]; // increment $line? // $line++; } ... You'll want to be incrementing $line somewhere though, unless you just want to keep overwriting $array_stuff[0] Another solution to this type of problem is to just read all the information into an array, then loop through that when you need it. E.G. $resultset = array(); while (mysql_fetch_array($result) as $row) { $resultset[] = $row; } Then just foreach ($resultset as $row) { // or ($resultset as $rownum = $row) // ... do stuff } Happy hunting. - Martin Norland, Database / Web Developer, International Outreach x3257 The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Using a loop on a result destroys array??
On Wednesday 24 November 2004 20:31, Mark Benson wrote: From the above code, Actually the above code would result in a parse error. There are no where-loops in PHP but there are while-loops. This is not being pedantic. If you're going to post code, make sure it's verbatim by using copy and paste your actual code. I cannot possibly do this as I am working on a commercially sensitive project and am instructed by my managing bodies not to quote actual code in case it contains sensetive data. Sorry if that is not how you like it but that is how I have to work. I can understand it must be frustrating for experienced members but alas I'm tied, I try my best... You want people to focus on your real problem and not on the mistakes you made in transcribing your actual code into your post. I actually write (rather badly it would seem!) theoretical examples that would (or in this case wouldn't!!) end in the same situation as the problem in my code (as I see it). loop 2 returns a blank array, the output is simply Array (). If I remove the loop (by commentiung it out) that extracts $crab the mysql $result array is suddenly full of the correct values. It seems to me that loop 1 is destroying or somehow not resetting $result. Very odd... mysql_data_seek() is what you need. Or you can consider storing the results into an array the first time round then you can read results from the array whenever next you need it. Thanks I'll try that. -- Mark Benson http://homepage.mac.com/markbenson -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Using a loop on a result destroys array??
On Wednesday, November 24, 2004, at 02:40PM, Norland, Martin [EMAIL PROTECTED] wrote: The real problem you're having is the statement says while there are results in this query, grab them - once you reach the end of the first while loop, there are no more results, so the second one is skipped right over - it's condition *never* evaluates true. So by running a while (mysql_fetch_array($result) loop over a fetch array you effectively empty it? If the query isn't changing, and there aren't other conditions/etc. - then just grab all the data the first time through and put it where it needs to go. OK, sounds like a plan... Sorry (again) about the code gaffs :-S -- Mark Benson http://homepage.mac.com/markbenson -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Using a loop on a result destroys array??
-Original Message- From: Mark Benson [mailto:[EMAIL PROTECTED] The real problem you're having is the statement says while there are results in this query, grab them - once you reach the end of the first while loop, there are no more results, so the second one is skipped right over - it's condition *never* evaluates true. So by running a while (mysql_fetch_array($result) loop over a fetch array you effectively empty it? snip Well, not really - although in the end, yes, it will be 'empty' - mysql_fetch_array actually fetches a single row (the next one, if available) returned from the mysql query into an associative array. You're being handed the data from mysql, one row at a time as requested, and sticking it into an array. $result is actually the resource id that you give mysql so it knows what query to give you the information back from. - Martin Norland, Database / Web Developer, International Outreach x3257 The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Using a loop on a result destroys array??
On Wednesday 24 November 2004 23:02, Mark Benson wrote: You want people to focus on your real problem and not on the mistakes you made in transcribing your actual code into your post. I actually write (rather badly it would seem!) theoretical examples Please don't construct theoretical examples. that would (or in this case wouldn't!!) end in the same situation as the problem in my code (as I see it). It's hard enough writing code that works without having to mess around with writing theorectical code that: doesn't work in the exactly the same fashion as another piece of code Construct a stripped-down, bare-bones, *working* example that illustrates your problem. (Working in the sense that it is free from syntax and other errors that are not the focus of the problem). -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-db -- /* Teachers have class. */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Using a loop on a result destroys array??
On Thu, 25 Nov 2004 01:36, Mark Benson wrote: On Wednesday, November 24, 2004, at 02:40PM, Norland, Martin [EMAIL PROTECTED] wrote: The real problem you're having is the statement says while there are results in this query, grab them - once you reach the end of the first while loop, there are no more results, so the second one is skipped right over - it's condition *never* evaluates true. So by running a while (mysql_fetch_array($result) loop over a fetch array you effectively empty it? Not really. What is happening is that each time you get a row from the result set, an internal pointer is advanced to the next row; if the final row has been requested, the pointer points to 'nowhere' and can't be further advanced without resetting it to the first row. Cheers -- David Robley I won't finish in fifth place, Tom held forth. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php