我有一个用 MySQL 准备的 PHP Web 应用程序,我决定通过 PDO 我遇到分页问题。
这是我的代码
<html>
<head>
<link rel="stylesheet" media="all" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/jquery-ui-timepicker-addon.css" />
<script src="js/jquery-1.7.2.min.js"></script>
<!-- jQuery UI -->
<script src="js/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript" src="js/timepicker.js"></script>
</head>
<script type="text/javascript" src="js/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function(){
$('#time').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: "HH:mm:ss "
});
minDate: getFormattedDate(new Date())
function getFormattedDate(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
});
</script>
<script type="text/javascript" src="js/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function(){
$('#time1').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: "HH:mm:ss "
});
minDate: getFormattedDate(new Date())
function getFormattedDate(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
});
</script>
<body>
<form action="" method="post">
<input class="input-medium" type="text" name="time" id="time" value="start_date"/>
<input class="input-medium" type="text" name="time1" id="time1" value="end_date"/>
<button style="margin-bottom:10px;" type="submit" class="btn btn-primary">Getir</button>
</form>
</body>
</html>
<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
$time = $_POST['time'];
$time1 = $_POST['time1'];
try {
require_once('conn.php');
$cepbankSUM = $conn->prepare("SELECT SUM(tutar) FROM cepbank WHERE tarih BETWEEN :time AND :time1");
$cepbankSUM->bindParam(':time', $time, PDO::PARAM_STR, 20);
$cepbankSUM->bindParam(':time1', $time1, PDO::PARAM_STR, 20);
$cepbankSUM->execute();
$row_cepbankSUM = $cepbankSUM->fetch();
// Find out how many items are in the table
$cepbank = $conn->prepare('SELECT COUNT(*) FROM cepbank WHERE tarih BETWEEN :time AND :time1');
$cepbank->bindParam(':time', $time, PDO::PARAM_STR, 20);
$cepbank->bindParam(':time1', $time1, PDO::PARAM_STR, 20);
$cepbank->execute();
$total = $cepbank->fetchColumn();
// How many items to list per page
$limit = 20;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = max($page - 1, 0) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
// Prepare the paged query
$stmt = $conn->prepare('SELECT * FROM cepbank WHERE tarih BETWEEN :time AND :time1 ORDER BY tarih DESC LIMIT :limit OFFSET :offset');
// Bind the query params
$stmt->bindParam(':time', $time, PDO:: PARAM_STR, 20);
$stmt->bindParam(':time1', $time1, PDO:: PARAM_STR, 20);
$stmt->bindParam(':limit', $limit, PDO:: PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO:: PARAM_INT);
$stmt->execute();
// Do we have any results?
if ($stmt->rowCount() > 0) {
// Define how we want to fetch the results
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt);
// Display the results
foreach ($iterator as $row_cepbank) {
?>
<table
<tbody>
<tr>
<td class="center"><?php echo $row_cepbank['username']?></td>
<td class="center"><?php echo $row_cepbank['trans_id']?></td>
<td class="center"><?php echo $row_cepbank['bank']?></td>
<td class="center"><?php echo $row_cepbank['tutar']?></td>
<td class="center"><?php echo $row_cepbank['operator']?></td>
<td class="center"><?php echo $row_cepbank['tarih']?></td>
</tr>
</tbody>
</table>
<?php }
} else {
echo '<p>No results could be displayed.</p>';
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?>
这是在第一次运行时使用第一个结果集没有任何问题的工作,当我单击下一页时出现此错误
« ‹ Page 0 of 0 pages, displaying -19-0 of 0 results › »
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20' at line 1
但是当我写时间变量manuel时
$time = '2013-08-01 00:00:00';
$time1 = '2013-09-01 00:00:00';
没有问题,一切都很完美,但是当我尝试使用 POST 方法通过 timepicker 获取时间时
$time = $_POST['time'];
$time1 = $_POST['time1'];
它失败。
等待你的伟大思想。
我已将偏移变量编辑为
$offset = max($page - 1, 0) * $limit;
不,结果是
当我点击下一页链接时,第一次运行它工作正常
« ‹ Page 0 of 0 pages, displaying 1-0 of 0 results › »
No results could be displayed.