odbc_num_rows() with MS Access always gives -1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

odbc_num_rows() with MS Access always gives -1

Post by Chris Corbyn »

Hi,

I'm trying to count the number of row in a Microsft Access database I've got.

I'm doing ti like this

Code: Select all

$user_odbc = "user";
$pass_odbc = "pass";
$database_odbc = "clientsDB";

$connect_db = odbc_connect($database_odbc,$user_odbc,$pass_odbc) or die ('Wrong Connect Details');

$query = "SELECT * FROM websites";

$result_odbc = odbc_exec($connect_db,$query);

$odbc_num_rows = odbc_num_rows($result_odbc);

echo $odbc_num_rows;
And no matter how many rows are in my table I always get the output as -1. Can I do this a more reliable way?

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

select count(*) from websites
?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Still returns -1. I've been Googling for this for hours. All the PHP manual has to say is
Note: Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
But if I don't use select to pull out my rows what else can I use?

The only thing I'm thinking is that I can read the highest value of the primary key and take that as the number of rows but it's a bit of botch-up.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not sure if this will work through ODBC but:

Code: Select all

SHOW TABLE STATUS LIKE 'table_name'
should have the auto_increment's next value in it..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'., SQL state 37000 in SQLExecDirect in d:\inetpub\wwwroot\add.php on line 13
Looks like the only queries I can make in ODBC with the Microsoft Access Driver are those shown above
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Who thinks doing it like this is just a bit silly? It works though...

Code: Select all

$user_odbc = "user";
$pass_odbc = "pass";
$database_odbc = "clientsDB";

$connect_db = odbc_connect($database_odbc,$user_odbc,$pass_odbc) or die ('Wrong Command!!!');

$query = "SELECT * FROM websites";

$result_odbc = odbc_exec($connect_db,$query);

$i = 0;
while (odbc_fetch_row($result_odbc)) {
    ++$i;
}

odbc_close($connect_db);

echo 'The number of rows in Table "websites" is '.$i;
It probably wont work faultlessly no doubt although I see no reason why not....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since it works, and you can't use any of the other systems, then that's the way to go I guess. :)
Post Reply