php - 从 MySQL 迁移到 MySQLi 扩展

标签 php mysql mysqli pagination

我有一个用 Mysql 编写的脚本,它只列出人名。它在 mysql 中工作正常,但我一直试图让它更新到 mysqli,我得到了大部分。但是,结果应该是分页的。分页工作正常,因为应该有 5 页,这就是它停止的地方......但所有结果都显示在每一页上。不确定我在做什么...任何帮助将不胜感激....我知道它与“mysql_result”有关

这是我在 mysqli 中的新代码:

<?php
include ('paginate.php'); //include of paginat page

$per_page = 10;         // number of results to show per page
$result = $con->query("SELECT EmployeeID,FirstName,LastName FROM employee;");
//If Database Error...Print the error and exit
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//If No Database Error.....Continue   
if ($result)
{
// Return the number of rows in result set
$total_results=mysqli_num_rows($result);
//printf("Result set has %d rows.\n",$total_results);
// Free result set
}
//total pages we going to have
$total_pages = ceil($total_results / $per_page);
//if page is setcheck
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page'];//it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;              
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View All Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text-   center"width="259" height="59" /></a>
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";                    
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";  
}
}
// close table>
echo "</table>";            
?>
</div>
</div>
</div>
</div>
</body>
</html>

这是我在 mysql 中的原始代码:

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_DEPRECATED);
include('config.php');    //include of db config file
include ('paginate.php'); //include of paginat page

$per_page = 10;         // number of results to show per page
$result = mysql_query("SELECT * FROM employee");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page'];             //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;              
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));

$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<style type="text/css">

</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text-center"width="259" height="59" /></a>
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'FirstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'LastName') . '</td>';
echo "</tr>";
}       
// close table>
echo "</table>";
// pagination
?>
</div>
</div>
</div>
</div>
</body>
</html>

我在这里做错了,我想不通....

任何帮助将不胜感激......

非常感谢。

最佳答案

我也将删除 while 语句并结合使用 mysqli_data_seekmysqli_fetch_accocMySQLi_data_seek 指向我需要的结果。检查一下:

// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First 

Name</th> <th>Last Name</th></tr></thead>"; 
// loop through results of database query, displaying them in the table 
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}

//go to the column you want the results 
mysqli_data_seek($result, $i);
$row = mysqli_fetch_assoc($result);
// echo out the contents of the row into a table
echo "<tr>";
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['LastName'] . '</td>';
echo "</tr>";  
}

}
echo "</table>"; 

关于php - 从 MySQL 迁移到 MySQLi 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26692046/

相关文章:

PHP/MySQL 文本到数据库表 - 查询命中但文本为空

php - 来自远程系统的 php 中的 XAMPP Mysql 连接错误

php - 如何解决 "Allowed memory size exhausted"错误?

php - Mysql输出一个而不是多个

javascript - 如何在 php while 循环中使用 javascript 来显示 map 标记

mysql - 组合多个 MySQL 选择查询的结果

mysql - 如何为具有多个 ORDER BY 子句的表建立索引?

php - 哪一个更好?使用您自己的加密算法还是使用 md5/sha1?

mysql - C - 预处理 mysql 数据,同时有效地维护文件写入的原始副本

php - 未知列 'STRICT_ALL_TABLES,' 设置 CodeIgniter DB 连接