简体   繁体   中英

PHP Oracle oci_num_rows result 0

I have a question about query in Oracle :

The problem is, the result of oci_num_rows is always 0.

and here is my code so far :

  $query = "SELECT IP_ADDRESS, STATUS FROM SEIAPPS_IP_ADDRESS WHERE IP_ADDRESS='$ip'";
  $result = oci_parse($c1, $query);
  oci_execute($result);
  echo $found = oci_num_rows($result);

To make it sure, I try to clear the condition "WHERE IP_ADDRESS='$ip'" . Still same with the result is 0 whereas in my table there are some data.

Any advice ?

Use this:

$query = "SELECT IP_ADDRESS, STATUS FROM SEIAPPS_IP_ADDRESS WHERE IP_ADDRESS='$ip'";
$result = oci_parse($c1, $query);
oci_execute($result);
$numrows = oci_fetch_all($result, $res);
echo $numrows." Rows";

This function does not return number of rows selected! For SELECT statements this function will return the number of rows, that were fetched to the buffer with oci_fetch*() functions.

Try this to check either your the credentials of your connection are correct or not.

<?php
$conn = oci_connect("hr", "welcome", "localhost/XE");
if (!$conn) {
    $e = oci_error();   // For oci_connect errors do not pass a handle
    trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
?>

For anyone else who fell into this trap, here's a simple summary of what the oci_fetch_all method will return.

$s = oci_parse($conn, "
          SELECT 1 AS A FROM DUAL 
    UNION SELECT 2 AS A FROM DUAL 
    UNION SELECT 3 AS A FROM DUAL 
    UNION SELECT 4 AS A FROM DUAL
");
oci_execute($s);
echo "records fetched: ". oci_num_rows($s)."\n";

oci_fetch($s);
echo "records fetched: ". oci_num_rows($s)."\n";

oci_fetch($s);
echo "records fetched: ". oci_num_rows($s)."\n";

oci_fetch_all($s, $out);
echo "records fetched: ". oci_num_rows($s)." out count: ". count($out['A']) . "\n";

will output:

records fetched: 0
records fetched: 1
records fetched: 2
records fetched: 4 out count: 2

So oci_num_rows is incremental, each call to oci_fetch will increment the counter by one (assuming there is at least one more record to fetch), and oci_fetch_all will fetch all remaining rows and put them into the $out buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM