Call to undefined function mysqli_result() [duplic

2020-04-11 07:43发布

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!

标签: php mysqli
1条回答
虎瘦雄心在
2楼-- · 2020-04-11 08:15

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);
查看更多
登录 后发表回答