Jump to content

Recommended Posts

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");
} 
Link to comment
https://forums.phpfreaks.com/topic/292292-running-while-loop-of-odbc-output-twice/
Share on other sites

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.

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.