Page 1 of 1

Get the record count before getting the records?

Posted: Tue May 27, 2003 8:22 am
by jiehuang001
I used the following code trying to display the total number of records and then dispaly all records. However, I can only be able to get the count number after the loop. How can I print the count number before printing the actual records?
-------------------the code--------------------------
$conn = // details omitted
$query = "select username, password from usertable";
$stmt = ociparse($conn,$query);
OCIDefineByName($stmt,"USERNAME",$username);
OCIDefineByName($stmt,"PASSWORD",$password);
OCIExecute($stmt);
while (OCIFetch($stmt)) {
print $username. " ". $password."/n";
}

$total_record = OCIRowCount($stmt);
print $total_record. " total records available".

Posted: Tue May 27, 2003 9:49 am
by twigletmac
Are you saying that:

Code: Select all

while (OCIFetch($stmt)) { 
    print $username. " ". $password."/n"; 
} 

$total_record = OCIRowCount($stmt); 
print $total_record. " total records available".
allows you to get the row count but

Code: Select all

$total_record = OCIRowCount($stmt); 
print $total_record. " total records available".

while (OCIFetch($stmt)) { 
    print $username. " ". $password."/n"; 
}
doesn't?

Mac

Get count number before getting records

Posted: Tue May 27, 2003 9:55 am
by jiehuang001
That is correct. You can try it yourself. The issue is that if you put
OCIRowCount() before the loop, it always returns 0.

Posted: Tue May 27, 2003 10:32 am
by twigletmac
Best place to start with problems is the manual:
http://php.net/ocirowcount
PHP manual - OCIRowCount() wrote:ocirowcount() returns the number of rows affected for eg update-statements. This function will not tell you the number of rows that a select will return!
Mac

Posted: Tue May 27, 2003 2:02 pm
by hedge
to get the count do a select count(*) query first. It doesn't know the count untill it is done fetching.