MySQLi分页

时间:2015-08-27 11:17:17

标签: php mysqli

之前使用MySQL扩展程序的代码已用过,我试图修改它以使用MySQLi扩展但无法使其正常工作。

代码中是否有明显的东西?

<?php

include($_SERVER["DOCUMENT_ROOT"]."/dbconnect.php");

$per_page = 20;

$pages_query = mysqli_query("SELECT count('company_id') AS company_count FROM companies ");

$pages = ceil(mysqli_result($pages_query, 0) / $per_page);

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;

$start = ($page - 1) * $per_page;

$sql  = "SELECT * FROM companies ";

$result = mysqli_query($conn, $sql);

$i = 0;

$dyn_table = "<table width='100%'>
    <tr>
    <th style='background-color:#FD8023; color:white; '></th>
    <th style='background-color:#FD8023; color:white; '>Company Name</th>
    <th style='background-color:#FD8023; color:white; '>Address</th>
    <th style='background-color:#FD8023; color:white; '></th>
    <th style='background-color:#FD8023; color:white; '></th>
    <th style='background-color:#FD8023; color:white; '></th>
    <th style='background-color:#FD8023; color:white; '>Post Code</th>
    <th style='background-color:#FD8023; color:white; '>Telephone No</th>
    <th style='background-color:#FD8023; color:white; text-align:center; '>Status</th>
    </tr>";

while($rows=mysqli_fetch_assoc($result)){

    if ($i % 2 == 0) {

$dyn_table .= "<tr>
    <td style=' background-color: #FD8023; '><a href='../company_amend/?company_id=".$rows['company_id']."'><i class=' alt fa fa-pencil'></i></a></td>
    <td>".$rows['company_name']."</td>
    <td>".$rows['address_1']."</td>
    <td>".$rows['address_2']."</td>
    <td>".$rows['address_3']."</td>
    <td>".$rows['address_4']."</td>
    <td>".$rows['post_code']."</td>
    <td>".$rows['telephone_no']."</td>";

$status=$rows['status'];

if ($status=="1") {
$dyn_table .= "<td style='text-align:center; color:#008000; '><i class='fa fa-check'></i></td>";
} else {
$dyn_table .= "<td style='text-align:center; color:#FF0000; '><i class='fa fa-times'></i></td>";
}

$dyn_table .= "</tr>";

    } else {

$dyn_table .= "<tr>
    <td style=' background-color: #FD8023; '><a href='../company_amend/?company_id=".$rows['company_id']."'><i class=' alt fa fa-pencil'></i></a></td>
    <td>".$rows['company_name']."</td>
    <td>".$rows['address_1']."</td>
    <td>".$rows['address_2']."</td>
    <td>".$rows['address_3']."</td>
    <td>".$rows['address_4']."</td>
    <td>".$rows['post_code']."</td>
    <td>".$rows['telephone_no']."</td>";

$status=$rows['status'];

if ($status=="1") {
$dyn_table .= "<td style='text-align:center; color:#008000; '><i class='fa fa-check'></i></td>";
} else {
$dyn_table .= "<td style='text-align:center; color:#FF0000; '><i class='fa fa-times'></i></td>";
}

$dyn_table .= "</tr>";

    }
    $i++;

}

mysqli_close($conn);

$dyn_table .= "</table>";

echo $dyn_table;

echo "<center>";
if ($page>1){ $first = 1; echo "<a href='?page=".$first."' class='button radius tiny'><i class='fa fa-angle-double-left'></i> First</a>&nbsp;&nbsp;&nbsp;";}
if ($page>1){ $previous = $page -1; echo "<a href='?page=".$previous."' class='button radius tiny'><i class='fa fa-angle-left'></i> Prev</a>&nbsp;&nbsp;&nbsp;";}
if ($page>=1 && $page < $pages){ $next = $page +1; echo "<a href='?page=".$next."' class='button radius tiny'>Next <i class='fa fa-angle-right'></i></a>&nbsp;&nbsp;&nbsp;";}
if ($page>=1 && $page < $pages){ $last = $pages; echo "<a href='?page=".$last."' class='button radius tiny'> Last <i class='fa fa-angle-double-right'></i></a>";}
echo "</center>";

?>

dbconnect.php:

<?php

$servername = "server";
$username = "admin";
$password = "password";
$dbname = "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

 ?>

我希望有人可以提供帮助。

非常感谢,

约翰

1 个答案:

答案 0 :(得分:1)

问题来自于SET @msum := 0; select t1.* from (select m.*, (@msum := @msum + m.oper_summ) as cumul_oper_summ from jos_fintools_data m order by m.data_date DESC ) t1 where t1.cumul_oper_summ <= 10000; (您的错误)。使用mysqli_result而不是数组,所以这是你必须使用它的方式

mysqli_fetch_array

$per_page = 20; $pages_query = mysqli_query($conn, "SELECT count('company_id') AS company_count FROM companies "); $pages = ceil(mysqli_fetch_array($pages_query)['company_count'] / $per_page); 返回所有结果的数组。在您的情况下,只有一个结果是Mysqli_fetch_array。所以只需将您想要使用的内容放在括号中