Ninjakreborn Posted October 19, 2009 Share Posted October 19, 2009 mysql_fetch_array returns 1 array per call. Generally that's why it is inserted into a while statement. I generally loop through them in a while statement and put them all in a master array so I have all the results in one place instead of having to use them on the spot in the while loop. So here is what I generally do: // If the flag all is set then run an "all" query. else we need to do some specifics on the query instead. <?php if ($flag['all'] == 1) { $sql = 'SELECT * FROM data_resources'; }else { $sql = ''; } #### Run Query #### $data = array(); //This is the master array that will contain all results. $query = mysql_query($sql); while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { $data[] = $row; } ?> If I echo out the data array inside the while statement it works properly, but when I try to access it outside of the while it doesn't work. What is going on? Normally this works just fine. Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 19, 2009 Share Posted October 19, 2009 we're going to need a bit more information than that. when/where are you trying to use the $data array afterward? and how does the $flag['all'] have anything to do with it here? have you run this bit of code both with $flag['all'] defined and without and received different results? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 Flag all is something else. It does not affect the code I am currently running. I have verified the code is running with the flag. It's not meant to run without. This is part of a much larger system, this is just one piece that is being built. For the time being I am trying to use the data array immediately after. I have an echo '<pre>'; print_r($data); echo '</pre>'; right after the while loop. If I do that inside the while loop it shows the array as it's being built..the first time through has $data[0] the second time through had $data[0] and $data[1] the third time is $data[0], $data[1], and $data[2] and so forth. However...when I do it right outside of the while loop however it's like the $data array isn't even set at this point for some reason? At the very least it should be detected the empty array at the top..strange situation. Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 19, 2009 Share Posted October 19, 2009 are you by any chance running this while() loop in a function, and the second data echo is outside the function? what, precisely, does the code you're running look like? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 That was the first thing I thought, but no. So far all the code is bits and pieces of a larger export system I am putting together. Now, the whole code your looking at now is under one action. I am using "action" parameters to decide what is going to happen on the page. All of this code however is under the same action Export_Data so that shouldn't have an effect. Beyond that there is nothing. I have a lot of parameters that are "going" to affect the query later on down the road (when the code is more developed) but for the time being this is the basic application. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 Just to simplify things here is the new code. <?php $sql = 'SELECT * FROM assets_resources'; #### Run Query #### $assets = array(); //This is the master array that will contain all results. $query = mysql_query($sql); while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { $assets[] = $row; echo '<pre>'; print_r($assets); echo '</pre>'; } echo '<pre>'; print_r($assets); echo '</pre>'; ?> The first print_r is shown correctly, the second one is not. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 using print_r() within a loop is pointless .. should really only be run once the loop has finished generating the array. there's really no reason the code posted above shouldn't produce the expected results .. has to be more to it that's not being shown .. what, i'm not sure at this point. when you say the second doesn't display correctly, what does that mean? Quote Link to comment Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 $assets = array(); //This is the master array that will contain all results. $query = mysql_query($sql); while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { $assets[] = $row; } echo '<pre>'; print_r($assets); echo '</pre>'; Works fine for me, as mrMarcus says the problem perhaps lies somewhere else. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 It's right there just as I posted it. The first one is there for testing...as I stated before. The second one is there for testing. The purpose of this is to find out why the first one is showing up and hte second one isn't. As I mentioned in the first post. I KNOW it is suppose to be, but it's not. No there is not more to it than is shown. As far as the issue I think I found out what it is, but why I don't know. When I run a full query (32,000 something results) it's not working in this location. When I limit it down to roughly 200 it works fine. I need to find a way to allow it to return all of the results. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 lose the print_r() from within the loop. it's not doing anything any good being in there, and it will produce the exact same result as the one outside of the loop .. all it's doing is re-printing the array over and over and over and over and over again, just with one more value added to the array each time .. might be that the array is too large at 32,000 results, i don't know. the code above (as cags posted), works. now i didn't try it on a recordset of 32,000 however, but it works nonetheless. and, do you really need an array containing 32,000 values? you might want to rethink that process as that could be quite a heavy load on the system, especially if numbers start to rise. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 I don't really have a choice. I need the results for a variety of different export types. If I was doing straight Export to xlS then it would be fine building the excel output straight away (which would work). What I need is have access to excel, filemaker, doc, pdf, csv, and a few other different types of exports. Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 19, 2009 Share Posted October 19, 2009 you could run the query piece-wise and add it to a temporary text file. Quote Link to comment Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 What version of PHP are you using, if your using 5.2 or newer you could try using memory_get_peak_usage() to find out how much memory your script is using. Try comparing that to the memory_limit value in your php.ini. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 I don't really have a choice. I need the results for a variety of different export types. If I was doing straight Export to xlS then it would be fine building the excel output straight away (which would work). What I need is have access to excel, filemaker, doc, pdf, csv, and a few other different types of exports. you could break up the export into separate dumps, with each file containing 5,000 records? 10,000? whatever your system can do. means the person will have to download several files to get the full results, but you save your system a serious workload. lot's of systems do that these days (break-up their file dumps into smaller files for export); Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 19, 2009 Author Share Posted October 19, 2009 you could run the query piece-wise and add it to a temporary text file. This is a good idea. Something to think about. you could break up the export into separate dumps, with each file containing 5,000 records? 10,000? whatever your system can do. means the person will have to download several files to get the full results, but you save your system a serious workload. lot's of systems do that these days (break-up their file dumps into smaller files for export); Another good idea, something else to think about. What version of PHP are you using, if your using 5.2 or newer you could try using memory_get_peak_usage() to find out how much memory your script is using. Try comparing that to the memory_limit value in your php.ini. A good idea, but not a valid solution in my situation. I don't really have authority to adjust the ini settings. Thanks again this should get me going. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2009 Share Posted October 19, 2009 memory_limit can be set in a script. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.