我正在尝试在以下代码中找到合适的代码来替换mysql_result
:
$per_page = 9;
$page_query = mysql_query("SELECT COUNT(*) FROM products");
$pages = ceil(mysql_result($page_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$result = mysql_query("SELECT * FROM products ORDER BY product_id DESC LIMIT $start, $per_page");
这就是我所做的,但我不确定它会起作用:
$per_page = 9;
$page_query = $conn->query("SELECT COUNT(*) FROM food");
$pages = ceil(fetch_assoc($page_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$sql = "SELECT * FROM food ORDER BY food_id DESC LIMIT $start, $per_page";
$pages = ceil(fetch_assoc($page_query, 0) / $per_page);
会有效吗?
答案 0 :(得分:2)
建议不要使用Mysqli。
这就是推荐PDO作为mysql ext替换
的原因$per_page = 9;
$count = $conn->query("SELECT COUNT(*) FROM food")->fetchColumn();
$pages = ceil($count / $per_page);
看,它比你的旧代码更简单。
以下是我写的PDO tutorial,您可以尝试看看它可以大大简化您的任务。
答案 1 :(得分:1)
尝试使用mysqli,在执行完查询后,您必须以数组或对象的形式获取,然后根据计数继续。在下面我提到了一些变化。试试吧,它可以帮到你
<?php
$per_page = 9;
$page_query = mysqli_query($conn, "SELECT COUNT(*) as count FROM products");
$row = mysqli_fetch_assoc($page_query);
$page = ceil($row['count'], 0) / $per_page);
$start = ($page - 1) * $per_page;
$result = mysqli_query("SELECT * FROM products ORDER BY product_id DESC LIMIT $start, $per_page");