Error when using: undefined constant num - assumed 'num'

I am following a php pagination tutorial that uses MYSQL, but I use a MYSQLI object oriented around my site. This causes 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 a fatal error: you cannot use an object of type mysqli_result as an array .. in 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 the use of the undefined constant num - assuming 'num' .. on the last line.

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

+4
source share
2 answers

change

 $total_pages = $row[num]; 

in

 $total_pages = $row['num']; 

you did quotation marks. Also note that the “undefined constant” error is just a notification , which means that your program should still work, but you must fix it. Always use quotes around the lines!

+4
source

$ lines ['Num'];

Single quotes

0
source

All Articles