Solved

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

Posted on 2003-12-04
8
215 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 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

809 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