views:

171

answers:

1

I'm using Oracle 10g Express on Windows XP, and on a Macbook OS X 10.5.8 running PHP 5.3.1 with PDO and Oracle Instant Client 10.2.0.4.0_4, I run the following script:

<?php
$pdo = new PDO("oci:dbname=winders:1521/xe", "system", "XXXXXX");
...

$sql = "WITH NumberedBugs AS (
    SELECT b.*, ROW_NUMBER() OVER (ORDER BY bug_id) AS RN 
    FROM Bugs b
) SELECT * FROM NumberedBugs WHERE RN = :offset";

$stmt = $pdo->prepare($sql);
$stmt->execute($offset);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

No problem. But I also want to be able to write a PHP script that gets the EXPLAIN PLAN report for this query. When I try this, I get an error:

$sql = "EXPLAIN PLAN FOR WITH NumberedBugs AS (
    SELECT b.*, ROW_NUMBER() OVER (ORDER BY bug_id) AS RN 
    FROM Bugs b
) SELECT * FROM NumberedBugs WHERE RN = 1234";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

(I changed the query parameter to a hard-coded integer literal for this test.)

I get this error when I try to call fetch():

ORA-24374: define not done before fetch or execute and fetch

What's the explanation of this, and how can I get the EXPLAIN PLAN report from a PHP script? I've done some web searches for this error, but I couldn't find a clear enough explanation.

+1  A: 

EXPLAIN PLAN FOR only writes the execution plan into a table (default plan_table).

To actually read the explain plan you could do a plain select from this table, or do a

SELECT * FROM TABLE(dbms_xplan.display);

after your first query to get a formatted output.

See Using EXPLAIN PLAN for more information.

Peter Lang
Do you mean I would `execute()` the first query but fetch no results, and then run a new query against `plan_table`? Or do I just need to `prepare()` the first query but not execute it?
Bill Karwin
Aha, I tried it. It seems I need to prepare and execute the first query, but I don't fetch any results. Then run the new query against either `plan_table` or `TABLE(dbms_xplan.display)`. They give results in different formats.
Bill Karwin
Yes, correct. `plan_table` contains all the information provided by `EXPLAIN PLAN`. `dbms_xplan.display` uses the data of the last query you explained and displays it in a format that's easier to read.
Peter Lang