note 11550 modified in function.ociparse by sixd
| From: | sixd@php.net | Date: | Mon, 05 Oct 2009 01:47:31 +0000 |
| Subject: | note 11550 modified in function.ociparse by sixd | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161311@lists.php.net to get a copy of this message | ||
It took me a while to work out the semantics of error handling for the Parse/Fetch/Execute seqeunce
as well.
OCIParse always returns true, so testing it is a waste of time. Instead, call OCIError, and test the
value of the "code" element.
OCINumRows always returns 1, BTW, so you have to loop. There are a number of other postings on this.
<?php
putenv("ORACLE_SID=admin");
putenv("ORACLE_HOME=/home/oracle");
// Kill the internal messages: this is DIY
error_reporting(0);
$conn = OCILogon("scott", "tiger");
$oerr = OCIError();
if ($oerr) {
if ($oerr["code"] == "1017")
echo "Please check your username and password";
else
echo "Error ".$oerr["message"];
exit;
} else {
echo "Hello";
}
// The table name here should be "emp", not "empxx"
$query = sprintf("select * from empxx");
/* Long version */
// Testing the result of the OCIParse call directly doesn't work...
$stmt = OCIParse($conn, $query);
if (!$stmt) {
$oerr = OCIError($stmt);
echo "Fetch Code 1:".$oerr["message"];
exit;
}
// However, if you test this, it performs as you would hope
$oerr = OCIError($stmt);
echo "Fetch Code 2:".$oerr["code"];
if ($oerr["code"]) {
echo "Error:".$oerr["message"];
exit;
}
// Or, you can catch it here (comment out the above block of code)
if (!OCIExecute($stmt)) {
$oerr = OCIError($stmt);
echo "Execute Code:".$oerr["code"];
if ($oerr["code"]) {
echo "Error:".$oerr["message"];
exit;
}
}
OCIFetch($stmt);
$ncols = OCINumCols($stmt);
$nrows = OCIRowCount($stmt);
printf("Result size is $ncols cols by $nrows rows.<br>");
for ($i=1; $i<=$ncols; $i++) {
printf("col[%s] = %s type[%d] = %s size[%d] = %s<br>",
$i, OCIColumnName($stmt, $i),
$i, OCIColumnType($stmt, $i),
$i, OCIColumnSize($stmt, $i));
}
$j=1;
do {
for ($i=1; $i<=$ncols; $i++) {
$col = OCIResult($stmt, $i);
printf("val[%d, %d] = %s * ", $j, $i, $col);
}
printf("<br>");
$j++;
} while (OCIFetch($stmt));
?>
--was--
It took me a while to work out the semantics of error handling for the Parse/Fetch/Execute seqeunce
as well.
OCIParse always returns true, so testing it is a waste of time. Instead, call OCIError, and test the
value of the "code" element.
OCINumRows always returns 1, BTW, so you have to loop. There are a number of other postings on this.
<?php
putenv("ORACLE_SID=admin");
putenv("ORACLE_HOME=/home/oracle");
// Kill the internal messages: this is DIY
error_reporting(0);
$conn = OCILogon("scott", "tiger");
$oerr = OCIError();
if ($oerr) {
if ($oerr["code"] == "1017")
echo "Please check your username and password";
else
echo "Error ".$oerr["message"];
exit;
} else {
echo "Hello";
}
// The table name here should be "emp", not "empxx"
$query = sprintf("select * from empxx");
/* Long version */
// Testing the result of the OCIParse call directly doesn't work...
$stmt = OCIParse($conn, $query);
if (!$stmt) {
$oerr = OCIError($stmt);
echo "Fetch Code 1:".$oerr["message"];
exit;
}
// However, if you test this, it performs as you would hope
$oerr = OCIError($stmt);
echo "Fetch Code 2:".$oerr["code"];
if ($oerr["code"]) {
echo "Error:".$oerr["message"];
exit;
}
// Or, you can catch it here (comment out the above block of code)
if (!OCIExecute($stmt)) {
$oerr = OCIError($stmt);
echo "Execute Code:".$oerr["code"];
if ($oerr["code"]) {
echo "Error:".$oerr["message"];
exit;
}
}
OCIFetch($stmt);
$ncols = OCINumCols($stmt);
$nrows = OCIRowCount($stmt);
printf("Result size is $ncols cols by $nrows rows.<br>");
for ($i=1; $i<=$ncols; $i++) {
printf("col[%s] = %s type[%d] = %s size[%d] = %s<br>",
$i, OCIColumnName($stmt, $i),
$i, OCIColumnType($stmt, $i),
$i, OCIColumnSize($stmt, $i));
}
$j=1;
do {
for ($i=1; $i<=$ncols; $i++) {
$col = OCIResult($stmt, $i);
printf("val[%d, %d] = %s * ", $j, $i, $col);
}
printf("<br>");
$j++;
} while (OCIFetch($stmt));
?>
http://php.net/manual/en/function.ociparse.php