Solved

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

Posted on 2003-12-04
8
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 167 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 166 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 167 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

751 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