1

I'm trying to output the data from a specific table within my Oracle SQL Developer database using a PHP form. I had some experience with this concept in the past using MySQL but I'm struggling to understand the differences between integrating it using Oracle specific syntax now.

My code is ideally laid out but when I run the code I get an error regarding the OCI_FETCH_ARRAY parameters, which I'm unsure on how to solve.

My Code

<?php
include("../connection.php");

error_reporting(E_ALL);

$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);

echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";

while($row= oci_fetch_array($stid, $sql))
{
echo "<tr>";
echo "<td>" . $row ['Algorithm_ID'] . "</td>";
echo "<td>" . $row ['Algorithm_Name'] . "</td>";
echo "<td>" . $row ['Algorithm_Role'] . "</td>";
echo "</tr>";
}

echo "</table>";

oci_free_statement($stid);
oci_close($conn);

?>

The Error I keep getting

Warning: oci_fetch_array() expects parameter 2 to be integer, string given in /studenthome.hallam.shu.ac.uk/STUDENTHOME8/1/b5040831/public_html/ADM/bin/php/entities/database.php on line 12

I get that it's asking for an integer, but why? In the MySQL concept you simply outline the connection string it was never an issue.

Can anyone help ?

0

1 Answer 1

3

Calling oci_fetch_array() ( from http://php.net/manual/en/function.oci-fetch-array.php) should be called like

while($row= oci_fetch_array($stid))

The second parameter is the mode - i.e. OCI_ASSOC.

Update: when using oci_parse(), this doesn't actually execute the parsed statement, you need to do...

oci_execute($stid);

So your code would be something like...

$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";

while($row= oci_fetch_array($stid, OCI_ASSOC))

(Look at http://php.net/manual/en/function.oci-parse.php for code examples)

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried changing it to while($row = oci_fetch_array($stid, OCI_ASSOC)) but i now get an error telling me ORA-24374: define not done before fetch or execute and fetch in
Added to answer about calling oci_execute($stid); after parse.
Some progress has been made, however now I am receiving Undefined Index errors for Algorithm_ID, Algorithm_Name and Algorithm_Role which repeats 3 times.
OK, try adding a print_r($row); just at the start of the while loop, see what data your getting.
I added it above the while loop, but the code doesn't recognise the variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.