tags:

views:

43

answers:

2

I'm following a php pagination tutorial that uses MYSQL but I use MYSQLI object oriented all around my site. This is causing some errors..

For this part..

$sql = "SELECT COUNT(*) as num FROM categories";
$total_pages = $connection->query($sql) or die(mysqli_error($connection)); 
$total_pages = $total_pages['num'];

I get *Fatal error: Cannot use object of type mysqli_result as array*.. on the last line

so I switched it to

$sql = "SELECT COUNT(*) as num FROM categories";
$total_pages = $connection->query($sql) or die(mysqli_error($connection)); 
$row = $total_pages->fetch_assoc();
$total_pages  = $row[num];

and now I get Use of undefined constant num - assumed 'num' ..on the last line.

At this point, I'm not sure what else to do. Can someone please help?

A: 

$row['num'];

Single quotes

Zane Edward Dockery
thanks I'm happy it was an easy fix!
Cyber Junkie
+2  A: 

change

$total_pages  = $row[num];

to:

$total_pages  = $row['num'];

you were mssing the quotes. Also, notice that the "undefined constant" error is just a notice, which means your program should still work but that you should fix it. Always use quotes around strings!

Mike Sherov
Mate, thanks for pointing that out! Yes, I noticed that it worked, which I found wierd.
Cyber Junkie
see "Why is $foo[bar] wrong?" at http://docs.php.net/language.types.array#language.types.array.foo-bar
VolkerK