Denis Milosavljevic Denis Milosavljevic - 1 month ago 21
PHP Question

Call to undefined function mysqli_result()

I have problem with mysqli_result() -> (ex mysql_result)
(Call to undefined function mysqli_result() )

My code:

$per_page = 6;
$pages_query = mysqli_query($conn, 'SELECT COUNT(id) FROM users');
$pages = ceil(mysqli_result($pages_query, 0) / $per_page);


browser error:

Fatal error: Call to undefined function mysqli_result() in /Applications/MAMP/htdocs/bootstrap/pagination.php on line 11

Thanks in advance!

Answer

Updated:

As opposed to mysql_result(), there's no mysqli_result() function available in MySQLi. Now there are two approaches to solve your problem.

Method(1):

Use mysqli_fetch_array() function to get the total number of rows. Your code should be like this:

$per_page = 6;
$pages_query = mysqli_query($conn, 'SELECT COUNT(id) FROM users'); 
$row = mysqli_fetch_array($pages_query);
$pages = ceil($row[0] / $per_page);

Method(2):

Alternatively, you can use mysqli_num_rows() to get the total number of rows from the result set. However, you need to change your query in the following way,

SELECT id FROM users

So your code should be like this:

$per_page = 6;
$pages_query = mysqli_query($conn, 'SELECT id FROM users');  
$pages = ceil(mysqli_num_rows($pages_query) / $per_page);