Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-12-04
8
Medium Priority
?
221 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
ID: 9880570
$result = mysql_query($sql);

$num_rows=mysql_num_rows($result);


0
 
LVL 24

Accepted Solution

by:
shivsa earned 501 total points
ID: 9880583
sorry for earlier post.

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

Author Comment

by:cas27
ID: 9880744
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:shivsa
ID: 9880789
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
 
LVL 5

Assisted Solution

by:Karitz
Karitz earned 498 total points
ID: 9881061
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 501 total points
ID: 9883455
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
ID: 9883740
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
ID: 9883826
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

926 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