Jump to content

PHP ADODB - Reuse Query Result


mcfc4heatons

Recommended Posts

I'm working on a PHP ADODB Project and want to reuse a query result and loop through results again, at the moment I am using the following approach, but assume this is inefficient and there must be a better approach?:

$query = "SELECT colname FROM table";
$result1 = $db->SelectLimit($query,10,-1);
$result2 = $db->SelectLimit($query,10,-1);

// 1ST RUN
while (!$result1->EOF) {
echo $result1->Fields('colname').'<br>';
$result1->MoveNext();
}
// 2ND RUN
while (!$result2->EOF) {
echo $result2->Fields('colname').'<br>';
$result2->MoveNext();
}

 

Link to comment
Share on other sites

You could just read the results into an array, then use that for your two loops.

$query = "SELECT colname FROM table";
$result1 = $db->SelectLimit($query,10,-1);
$resultData=[];
while (!$result1->EOF) {
    $resultData[]=$result1->Fields('colname');
    $result1->MoveNext();
}

// 1ST RUN
foreach ($resultData as $colname){
    echo $colname.'<br>';
}

// 2ND RUN
foreach ($resultData as $colname){
    echo $colname.'<br>';
}

 

Link to comment
Share on other sites

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.