加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在mysqli中转换mysql_result?

发布时间:2020-12-13 16:04:26 所属栏目:PHP教程 来源:网络整理
导读:这个代码以前是在 mysql中,现在因为它已被弃用,我决定在mysqli中转换我的代码,但是我在我的页面中有这个问题有分页,在它使用mysql之前没有错误,但现在我得到了这一行出错: Warning: mysqli_fetch_assoc() expects exactly 1 parameter,2 given 错误是显而易
这个代码以前是在 mysql中,现在因为它已被弃用,我决定在mysqli中转换我的代码,但是我在我的页面中有这个问题有分页,在它使用mysql之前没有错误,但现在我得到了这一行出错:

Warning: mysqli_fetch_assoc() expects exactly 1 parameter,2 given

错误是显而易见的,我知道,但我不知道如何以另一种方式做到这一点,因为以前我的代码在那一行是

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

我也尝试过这个

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

但是我得到了这个错误

Notice: Object of class mysqli_result could not be converted to int

所以我想知道如何将mysql_ * mysql_result转换为mysqli?或者还有其他方法吗?

到目前为止我的代码是这样的:

<?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)) {
....
}
?>

解决方法

问题在于这行,你不能直接使用mysqli_query()而不需要获取数据

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

mysqli_query()

Returns FALSE on failure. For successful SELECT,SHOW,DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object .

它会是的

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

您需要从查询对象中获取数据

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读