Solved

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

Posted on 2003-12-04
8
213 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:cas27
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:shivsa
Comment Utility
$result = mysql_query($sql);

$num_rows=mysql_num_rows($result);


0
 
LVL 24

Accepted Solution

by:
shivsa earned 167 total points
Comment Utility
sorry for earlier post.

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

Author Comment

by:cas27
Comment Utility
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
0
 
LVL 24

Expert Comment

by:shivsa
Comment Utility
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))

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Assisted Solution

by:Karitz
Karitz earned 166 total points
Comment Utility
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";
?>
0
 
LVL 6

Assisted Solution

by:aolXFT
aolXFT earned 167 total points
Comment Utility
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.



0
 

Author Comment

by:cas27
Comment Utility
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
0
 
LVL 6

Expert Comment

by:aolXFT
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now