Page 1 of 1

Newbie Access Database question

Posted: Fri Dec 06, 2002 4:09 pm
by lhamlin
Hi,
I have just started working with PHP (have 4 resource books opened simultaneously). I am trying to convert an application that my company wrote in ColdFusion to PHP. The demo database that we distribute is MS Access. I have been trying to run a query on a table, and do the following:

1) get the record count
2) put the results into an array for further processing.

The problem I seem to be having (I think....) is that whichever order I try
(1 followed by 2, or 2 followed by 1) it seems I am only getting valid results for the first step I execute.

I am using the following function to get the recordcount:

function RecordCount ($Query) {
while (odbc_fetch_row($Query)) $rows++;
odbc_fetch_row($Query,0);
return $rows;
}


for the array, I am using:

while(odbc_fetch_row($getuser)) {

$odbcFieldName=odbc_field_name($getuser,1);
$LocalAccountList[$odbcFieldName]=odbc_result($getuser,1);
}

If I do the record count first, I get the correct record count, but then I don't get any results in the array. If I do the array first, I get results in the array, but the record count comes back blank.

Any help would be greatly appreciated as I am starting to lose sleep (and hair) over this. :oops:

Regards,
Lisa

Posted: Fri Dec 06, 2002 4:26 pm
by hedge
I am just guessing here but I think you need to 'reset' the cursor somehow. Your recordcount loop has fetched all the rows and therefore the pointer is at the bottom and there are no more rows.

BTW that is very inefficient, either get the recordcount using select count(*) or count them as you fetch, you don't want to fetch twice.

Posted: Sat Dec 07, 2002 2:19 pm
by f1nutter
The count and assignment can be done in one go.

I have not used ODBC so pick the bits from this!

Code: Select all

<?php

$connection = odbc_connect ($dsn, $user, $password);
$result = odbc_exec ($connection, $query_string);
$rows = odbc_num_rows ($result);

while(odbc_fetch_row($result))
{
 $odbcFieldName=odbc_field_name($result,1);
 $LocalAccountList[$odbcFieldName]=odbc_result($result,1);
} 
?>
However, this will continue to overwrite the contents of $LocalAccountList as it will be an array of 1 element, indexed by $odbcFieldName, which looks like it doesn't change. You might also like to think about using column names, in case the order of the columns change.