【问题标题】:Migration from MySQL to MySQLi extension从 MySQL 迁移到 MySQLi 扩展
【发布时间】:2014-12-28 19:10:42
【问题描述】:

我有一个用 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>

我在这里做错了,我无法弄清楚....

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

非常感谢。

【问题讨论】:

  • 请尝试将您的代码简化为仍然存在问题的最小示例;这不仅可以帮助我们,还可以帮助您自己找到答案。见stackoverflow.com/help/mcve
  • 我正在尝试将 myqsl_result 转换为 mysqli....它正在工作,但结果都显示在同一页面上...而不是 paginatd
  • 有很多遗漏:所有变量从何而来?例如。 $total_results.
  • 我将编辑问题以显示变量...

标签: php mysql mysqli pagination


【解决方案1】:

您提取查询的方式是问题所在。一旦你输入while statement,它不会离开,直到所有结果都完成,然后它才会执行

if ($i == $total_results) {
    break;
}

所以它永远不会有机会以大于 $total_result 的 $i 结束。修复方法是在查询中包含 LIMIT 和 OFFSET。这意味着您也必须修复分页页面。

所以只需像这样再次运行查询:

// 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++) {
// run the query again

    $sql ="SELECT EmployeeID,FirstName,LastName FROM employee LIMIT $per_page OFFSET $end;
    $result = $con->query($sql);
    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>";  
    }

}
echo "</table>";            

【讨论】:

  • 我需要查看分页代码。但是,是的,我可以提供帮助
【解决方案2】:

我也将删除 while 语句并将 mysqli_data_seek 与 mysqli_fetch_accoc 结合使用。 MySQLi_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>"; 

【讨论】:

  • 这很完美....我会选择它作为正确答案....非常感谢您的帮助。
  • 您检查过其他解决方案吗?这个答案发生了什么。是的,我会投票给你 q。
  • 其他解决方案不起作用。它仍然列出了每一页上的所有名字……它会分页……但是所有的名字都出现在每一页上。这个工作很完美......再次......非常感谢。
猜你喜欢
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2010-10-20
  • 2015-06-03
  • 1970-01-01
  • 2016-01-08
  • 2017-12-08
相关资源
最近更新 更多