简体   繁体   English

如何在mysqli中转换mysql_result?

[英]how to convert mysql_result in mysqli?

This code was previously in mysql and now since it is deprecated, I decided to convert my code in mysqli, but I'm having this problem in my page where there is pagination, before it works with mysql with no error, but now I get an error in this line: 这个代码以前是在mysql中,现在因为它已被弃用,我决定在mysqli中转换我的代码,但是我在我的页面中有这个问题有分页,在它使用mysql之前没有错误,但现在我得到了这一行出错:

Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given 警告:mysqli_fetch_assoc()只需要1个参数,给定2个

The error is obvious, I know that, but I don't know how to do it the other way because previously my code in that line is 错误是显而易见的,我知道,但我不知道如何以另一种方式做到这一点,因为以前我的代码在那一行是

$pages = ceil(mysql_result($pages_query, 0) / $limit); // total number of pages

I also tried this 我也尝试过这个

$pages = ceil($pages_query / $limit); // total number of pages

but I get this error 但是我得到了这个错误

Notice: Object of class mysqli_result could not be converted to int 注意:类mysqli_result的对象无法转换为int

So I'm wondering how will I convert the mysql_* mysql_result to mysqli? 所以我想知道如何将mysql_ * mysql_result转换为mysqli? or is there other way to do it? 或者还有其他方法吗?

So far my code is this: 到目前为止我的代码是这样的:

<?php
include 'dbcontroller.php';

$limit = 10; // limit the number of post per page
$pages_query = mysqli_query($conn, "SELECT COUNT('id') FROM news");
$pages = ceil($pages_query / $limit); // total number of pages

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit; // set the page to 0
$query = mysqli_query($conn, "SELECT * FROM news ORDER BY date DESC LIMIT $start, $limit"); 


while($row = mysqli_fetch_array($query)) {
....
}
?>

Problem is with this line you can't use mysqli_query() directly without fetching data 问题在于这行,你不能直接使用mysqli_query()而不需要获取数据

$pages = ceil($pages_query / $limit); // total number of pages

mysqli_query() mysqli_query()

Returns FALSE on failure. 失败时返回FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object . 对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。

SO it would be 它会是的

$pages_query = mysqli_query($conn, "SELECT COUNT('id') FROM news");

You need to fetch data from your query object 您需要从查询对象中获取数据

     $row = mysqli_fetch_array($pages_query);// fetch fata
     $ic = $row[0];
     $pages = ceil($ic / $limit); // total number of pages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM