JBRTaylor Posted November 5, 2014 Share Posted November 5, 2014 Hi I am having problems running a while loop twice. The first loop runs fine but the 2nd one does not run. Can anyone please advise why this is? My code is below Thanks in advance. Jonathan $sql = "SELECT id, url, time FROM fyi_links"; $res = odbc_exec($con, $sql); while ($row = odbc_fetch_array($res)) { print($row['id'].",".$row['url'].",".$row['time']."\n"); } //Run loop again after some other code while ($row = odbc_fetch_array($res)) { print($row['id'].",".$row['url'].",".$row['time']."\n"); } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 5, 2014 Share Posted November 5, 2014 i'm not sure that the obdc_ database library has a 'seek' function that works universally, that would let you iterate over the same result set more than once, but you should be decoupling your database layer from your presentation layer, by storing the result from the database query in an array for the presentation code to use, that's totally independent of the type of database library functions being used. if you are going to output the exact same content more than once, you would want to produce and store the output in a php variable, then simply echo it wherever you want. Quote Link to comment Share on other sites More sharing options...
JBRTaylor Posted November 5, 2014 Author Share Posted November 5, 2014 Hi, thanks for the quick reply. I am a beginner as u probably guessed, to help me understand could u provide an example of adding the array to a variable and then echoing it in a similar way to my example. Thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 6, 2014 Share Posted November 6, 2014 to store the result in an array, that you could then loop over multiple times or loop over once and store the produced output in a php variable - $rows = array(); // array to hold the fetched data while ($row = odbc_fetch_array($res)) { $rows[] = $row; } // use the $rows array any what you want here. you can also use count($rows) to get a count of the number of rows the query matched, that is database neutral. 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.