*** URGENT *** How do I check for a "0 rows returned" message in Oracle 8 via PHP?

Hi experts,

I am using Oracle8 and PHP for a web project and I need to know how I can tell when my query returns 0 rows.

I have look at calling OCIRowCount after the OCIFetch, but it returns 1, row even with no data in the table.

Any ideas on how I can do this?

TIA,

Craig
cas27Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shivsaCommented:
$result = mysql_query($sql);

$num_rows=mysql_num_rows($result);


shivsaCommented:
sorry for earlier post.

$nrows = ocifetchstatement($parsed, $results);
   echo "Found: $nrows results<br><br>\n";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cas27Author Commented:
umm...  I tried it, and it is returning 4 rows, but when I run the query on the server it gives me 0 rows (since the table is empty).

Here is the code that I am using:
------------------------------------------------------------------------------------
if (!$con = @OCILogon("name", "pass", $db))
{
//      $url = "error.php?error=parse";
//      echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$url\">";
}

$query = "SELECT * FROM stupid";

//can we parse the statement?
if (!$stmt = @OCIParse($con, $query))
{
//      $url = "error.php?error=parse";
//      echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$url\">";
}

//execute the query
if (! OCIExecute($stmt))
{
//      $url = "error.php?error=parse";
//      echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$url\">";
}
?>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
      $nrows = OCIFetchStatement($stmt, $results);
      echo "Found: $nrows results<br><br>\n";
?>

-------------------------------------------------------------------------------------

Am I doing something dumb and not realizing it (I just started learning PHP 2 weeks ago so its certainly possible :-)

Thanks,
Craig
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

shivsaCommented:
change this
if (!$con = @OCILogon("name", "pass", $db))
to
if (!$con = OCILogon("name", "pass", $db))

and

change this
if (!$stmt = @OCIParse($con, $query))
to
if (!$stmt = OCIParse($con, $query))

KaritzCommented:
Your code looks ok


but you can check for zero row result by adding some code like

<?
     $nrows = OCIFetchStatement($stmt, $results);
     if ($nrows==0)    {
        echo "Query is empty";
}
 echo "Found: $nrows results<br><br>\n";
?>
aolXFTCommented:
Oracle doesn't have a *num_rows() function or an equivlent.

PEAR's DB module emulates it as follows

    function numRows($result)
    {
        // emulate numRows for Oracle.  yuck.
        if ($this->options['optimize'] == 'portability' &&
            $result === $this->last_stmt) {
            $countquery = preg_replace('/^\s*SELECT\s+(.*?)[,\s].*\s+FROM\s+/is',
                                       'SELECT COUNT(\1) FROM ',
                                       $this->last_query);
            $save_query = $this->last_query;
            $save_stmt = $this->last_stmt;
            $count = $this->query($countquery);
            if (DB::isError($count) ||
                DB::isError($row = $count->fetchRow(DB_FETCHMODE_ORDERED)))
            {
                $this->last_query = $save_query;
                $this->last_stmt = $save_stmt;
                return $this->raiseError(DB_ERROR_NOT_CAPABLE);
            }
            return $row[0];
        }
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
    }

Basicly what it does is creates a new query based on the old one:

           $countquery = preg_replace('/^\s*SELECT\s+(.*?)[,\s].*\s+FROM\s+/is',
                                       'SELECT COUNT(\1) FROM ',
                                       $this->last_query);

This means that you have to 'remember' the last query, and it has to be a select query(although I'm not sure if any other query returns any rows).

Hmmm one way might be to migrate to the DB module, and simply use the above function, since it automaticly 'remembers' queries.



cas27Author Commented:
First off - huge thanks to you all for responding so quickly.

Shivsa & Karitz - thanks for the help,  but unfortunately it doesn't appear to be working the way I want it to.

aolXFT - I was afraid I was going to have to do something like that, but I will just write the query to return the count as well as the column I want - since I'm selecting the PK it'll work ok.

I'll split points amongst the 3 of you for helping me out.  I have not tried to split points 3 ways, so if I can only split between 2 people, I will split between Shivsa and Karitz first, then post another 250 pt, question for aolXFT.

Sound fair?

Thanks again,

Craig
aolXFTCommented:
Seems fair enough to me.

I don't use oracle, but in mysql I'd just loop through the rows, and increment a counter each time. Although from your code, it seems that all you want is the amount of rows, and not the content of the rows, in which case you're probably better doing a count anyway.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.