Asriel Repp Asriel Repp - 2 months ago 17
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;

|COUNT(id)|
|9 |


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

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


Any ideas why this is happening?

Answer

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];