Asriel Repp Asriel Repp - 11 months ago 80
MySQL Question

mysqli_fetch_array() returning NULL instead of value of COUNT()

I'm using the code

SELECT COUNT(id) FROM blog.posts WHERE hidden='0'
to find out how many pages to generate for pagination on a website. This query returns the following table;

|9 |

But I've ran into an issue. My PHP script to handle this information echos ''. Instead of the expected value '9'.

echo $pages[0];

Any ideas why this is happening?

Answer Source

The parameter for the mysqli_fetch_array() or any of the mysqli_fetch_* api calls should be a mysqli_result and not a connection object

$result = mysqli_query($conn,$sql);
$pages = mysqli_fetch_array($result);    //<--- change
echo $pages[0];