Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 23rd, 2003, 06:20 AM
Registered User
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to count Records using acess as backend in Php

Following is the code :
When i print the value of cntr it is not displaying any value
I have 4 records in the database how do i get the number of records without using while loop , Using count property .

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=test.mdb");

    $rs = $conn->Execute("SELECT count(loginid) FROM login");
    print "The Counter is ==".$cntr;

Old December 23rd, 2003, 04:28 PM
richard.york's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

I don't think you yet have enough 'meat' in your code to get a count of the records.

If I were you I'd have a look at the user contributed notes for the COM class.


Take this example for instance, taken from the user-contributed notes for the COM class page:
quote:From: http://www.php.net/manual/en/class.com.php

If you want to use a DSN-less connection to a database (access for example) and return the results of a query as a multidimensional array with the fieldnames as identifier try this. Hope it helps someone :-)

function db($sql) {
   $c = new COM("ADODB.Connection");
   $c->open('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' . realpath("relative_path/db.mdb"));
   $r = $c->execute($sql);
   $i = 0;
   $num_fields = $r->fields->count();
   while (!$r->EOF)
           for($j = 0;$j<$num_fields;$j++) {
           $key = $r->Fields($j);
           $r_items[$i][$key->name] = $key->value;
   $r = null;
   $c = null;
   return $r_items;

use it like this:
$results = db("SELECT field_a, field_b FROM table");
foreach($result as $i => $item) {
   echo $item['field_a'];
   echo $item['field_b'];
You can also use: print_r($result); if you want the structure of the array to be printed.
In this example the results of the query are returned in a multidimensional associative array. This suggests that you can get a count of the records by doing this:

$cntr = count($results);

There are several examples of accessing an access database via COM on that page.

: )

Smiling Souls

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Variables to Count Unique Records Dallas Miller BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 0 October 18th, 2007 08:03 AM
Pls Help me count records [soon after connection] amjad_mahmood VB Databases Basics 1 August 30th, 2005 07:20 PM
Count of form records siptah Access VBA 3 April 20th, 2005 09:09 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
Listing records based on a count harg7769 Access 3 April 8th, 2005 03:15 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.