At my workplace, I am given a project that’s written in ColdFusion, and I need to convert it to PHP, using Oracle as the back-end. While converting one of the files, I came across a very good (mind engaging) puzzle, which was to this effect:
The page needs to have 5 drop-down select boxes, and each drop-down menu should be populated with a list of “items” gathered/fetched from the Database.
Now, if you are a dirty/lazy coder, you can just go-ahead and loop over an fetched array 5 times, creating 5 select boxes each time, however, a smarter solution would be to “reuse” the code!
So, non-lazy coder that I am (:p), I came up with the following solution:
-
for($cntr = 1; $cntr <= 5; $cntr += 1){
-
echo '
-
<tr>
-
<td align=left>
-
<select name="Name'.$cntr.'"><br />
-
<option value="none"> </option>';
-
while($resArr = oci_fetch_array($stmt, OCI_BOTH)){
-
$ID = $resArr['IID'];
-
$Item = $resArr['INAME'];
-
echo '<option value='.$ID.' />'.$Item.'
-
</option>';
-
}
-
echo '
-
</select>
-
</td>
-
</tr>';
-
}
However, no matter how good it looked in my mind when I first thought of this, in the end (after writing and subsequently running the code), I found out that it did the first drop-down list just fine, but for the next 4, it crapped out, giving me the ORA-01002 error. I kept banging my head to the desk (semi-literally), and then it hit me! (no… not the desk; the desk hit me when I semi-literally banged my head against it to begin with).
So, in case you are trying to achieve something similar to mine, and keep getting the very annoying error:
Warning: oci_fetch_array() [function.oci-fetch-array]: ORA-01002: fetch out of sequence in /some/path/filename.php on line 81
Just do the following:
Execute the resource statement within the for-loop. In other words, right after the for statement: insert the following statement on the 2nd line in the code above
-
for($cntr = 1; $cntr <= 5; $cntr += 1){
-
oci_execute($stmt);
-
…
-
…
Hopefully, this has helped.
-RushiKumar
Popularity: 1% [?]
Posted by
Under
Tags: 
No Comment Received
Leave A Reply