Noel ST Noel ST - 2 months ago 6
SQL Question

Number of rows obtained from an sql query from php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>

</head>

<body>

<?php
// extract all the form fields and store them in variables
$WineType=$_POST['WineType'];
$Pairing=$_POST['Pairing'];
$PriceRange=$_POST['PriceRange'];

// the table will always have at three columns
$tablewidth = 600;

$sqlstatement = "SELECT * FROM wine1";

/* Set oracle user login and password info */
$dbuser = "xxxx"; /* your login */
$dbpass = "xxxx"; /* your password */
$db = "SSID";
$connect = oci_connect($dbuser, $dbpass, $db);

if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}

/* build sql statement using form data */
$query = $sqlstatement;

/* check the sql statement for errors and if errors report them */
$stmt = oci_parse($connect, $query);

echo "SQL: $query<br>";

if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
oci_execute($stmt);


$howmany=0;
while(oci_fetch_array($stmt)) {
$howmany=$howmany+1;
}

echo "Number of items: $howmany<br>";
?>

<?php
oci_close($connect);
?>

</body>
</html>


Hello, for the above code. I am trying to count the number of rows returned from an oracle server when i run the "SELECT * from wine1;" query.

It seems the the $count is zero after running the code

I have confirmed that I am able to retrieve all six items stored in the table when submitting the query in the SQL server itself. It just doesn't seem to work in php.

Answer

Use this in your case

$numrows = oci_fetch_all($stmt, $res);
echo 'Total rows: '.$numrows;

for iterating all records use this

foreach($res as $rows) { 
  var_dump($rows); 
}

Reference http://php.net/manual/en/function.oci-fetch-all.php

Comments