Get the record count before getting the records?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

Get the record count before getting the records?

Post 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".
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

Get count number before getting records

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

to get the count do a select count(*) query first. It doesn't know the count untill it is done fetching.
Post Reply