开发者

Getting error: Use of undefined constant num - assumed 'num'

开发者 https://www.devze.com 2023-01-09 12:35 出处:网络
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..

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 开发者_如何学JAVAnum 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?


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!


$row['num'];

Single quotes

0

精彩评论

暂无评论...
验证码 换一张
取 消