Issue in OCI/PHP using oci_num_rows

2019-09-07 04:40发布

问题:

Why would the below show 0 results returned, when the exact same query via SQL returns 228?

error_reporting(E_ALL);
ini_set('display_errors', 1);

include("connection.php");

if($conn){

  $stid = oci_parse($conn, "SELECT COUNT(CustomerNo) FROM Customers");
  oci_execute($stid);

  echo oci_num_rows($stid) . " rows returned.<br />\n";

  oci_free_statement($stid);
  oci_close($conn);
}

回答1:

You have to fetch the results and then call oci_num_rows. As described on the PHP manual for oci_num-rows -

Note: 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.

Therefore your PHP code should be like:

error_reporting(E_ALL);
ini_set('display_errors', 1);

include("connection.php");

if($conn){

  $stid = oci_parse($conn, "SELECT COUNT(CustomerNo) FROM Customers");
  oci_execute($stid);

  $numrows = oci_fetch_all($stid, $res);
  echo $numrows . " rows returned.<br />\n";

  oci_free_statement($stid);
  oci_close($conn);
}


标签: php oci