我需要使用mysqli以程序化的方式做事。尚未学习oop风格或pdo。
我正在尝试创建一个分页页面,其中10页显示所有记录。记录分布在10页以上。 即使我的tbl有记录,我也注意到没有记录存在。 html表中没有显示任何记录。 那是为什么?
以下是我尝试使用mysqli使用PREP STMT程序样式显示记录。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title><?php echo "$site_name $user"; ?> User's Notices in <?php echo
$server_time; ?> time.</title>
</head>
<body>
<br>
<center><span style="font-weight: bold;"><?php echo "$site_name $user"; ?>
User's Notices in <?php echo $server_time; ?> time.</span></center>
<br>
<br>
<?php
$query = "SELECT id,date_and_time,recipient_username,sender_username,notice
FROM notices WHERE recipient_username = ?";
if ($stmt = mysqli_prepare($conn,$query))
{
//Bind Parameter
mysqli_stmt_bind_param($stmt,'s',$recipient_username);
//Execute Statement
mysqli_stmt_execute($stmt);
//$result_1 = mysqli_query($conn,$query);
//Bind Result Variables
$result_1 = mysqli_stmt_bind_result
($stmt,$id,$date_and_time,
$recipient_username,$sender_username,$notice);
mysqli_stmt_store_result($stmt);
$rows_num = mysqli_stmt_num_rows($stmt);
printf("Result set has %d rows.\n",$rows_num);
$page_count = 10;
$page_size = ceil($rows_num / $page_count);
//Get the Page Number, Default is 1 (First Page).
$page_number = $_GET["page_number"];
if ($page_number == "") $page_number = 1;
$offset = ($page_number -1) * $page_size;
$query .= " limit {$offset},{$page_size}";
?>
<table width="1500" border="0" cellpadding="5" cellspacing="2"
bgcolor="#666666">
<?php if($rows_num) {?>
<tr name="headings">
<td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission
Number</td>
<td bgcolor="#FFFFFF" name="column-heading_logging-server-date-&-time">Date
& Time in <?php echo $server_time ?></td>
<td bgcolor="#FFFFFF" name="column-heading_to">To</td>
<td bgcolor="#FFFFFF" name="column-heading_from">From</td>
<td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td>
</tr>
<?php while($row = mysqli_stmt_fetch($stmt))
{
?>
<tr name="user-details">
<td bgcolor="#FFFFFF" name="submission-number"><?php echo $row['id']; ?>
</td>
<td bgcolor="#FFFFFF" name="logging-server-date-&-time"><?php echo
$row['date_and_time']; ?></td>
<td bgcolor="#FFFFFF" name="recipient_username"><?php echo
$row['recipient_username']; ?></td>
<td bgcolor="#FFFFFF" name="sender_username"><?php echo
$row['sender_username']; ?></td>
<td bgcolor="#FFFFFF" name="notice"><?php echo $row['notice']; ?></td>
</tr>
<?php
}
?>
<tr name="pagination">
<td colspan="10" bgcolor="#FFFFFF"> Result Pages:
<?php
$rows_num = mysqli_stmt_num_rows($stmt);
if($rows_num <= $page_size)
{
echo "Page 1";
}
else
{
for($i=1;$i<=$page_count;$i++)
echo "<a href=\"{$_SERVER['PHP_SELF']}?page_number={$i}\">{$i}</a> ";
}
?>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td bgcolor="FFFFFF">No record found! Try another time.</td>
</tr>
<?php
}
?>
</table>
<br>
<br>
<center><span style="font-weight: bold;"><?php echo "$site_name $user"; ?>
User's Notices in <?php echo $server_time; ?> time.</span></center>
<br>
<br>
</div>
<br>
</body>
</html>
<?php
}
//Free Result Set
mysqli_stmt_free_result($stmt);
//Close Database Connection
mysqli_stmt_close($stmt);
?>
我得到了回应:
结果集有0行。 没有找到记录!再试一次。
这是错误的。 mysql tbl中有记录。
忽略过时的htmls,因为我稍后会将它们升级到html 5。目前,我正在修复php。
答案 0 :(得分:0)
这是我用于分页的内容。它也更灵活,只需将其固定为10页。
// Get inital params
if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
$max_results = 100;
$offset = $page*$max_results - $max_results;
// This is the maximium amount of pages to display before adding Prev \ Next
$max = 10;
$query = "SELECT id FROM notices WHERE recipient_username = ?";
if ($stmt = mysqli_prepare($conn,$query)) {
mysqli_stmt_bind_param($stmt,'s',$recipient_username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$records = mysqli_stmt_num_rows($stmt);
mysqli_stmt_close($stmt);
}
function get_paging_info($count, $offset, $curr_page)
{
$pages = ceil($count / $offset);
$data = array();
$data['si'] = ($curr_page * $offset) - $offset;
$data['pages'] = $pages;
$data['curr_page'] = $curr_page;
return $data; //return the paging data
}
$paging_info = get_paging_info($records, $max_results, $page);
$html = "";
if($paging_info['curr_page'] > 1) {
$html = $html . "<a href='1' title='Page 1'>First</a> <a href='" . ($paging_info['curr_page'] - 1) . "' title='Page " . ($paging_info['curr_page'] - 1) . "'>Prev</a>";
}
if($paging_info['curr_page'] < $max) {
$sp = 1;
} else {
if($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) ) {
$sp = $paging_info['pages'] - $max + 1;
} else {
if($paging_info['curr_page'] >= $max) {
$sp = $paging_info['curr_page'] - floor($max/2);
}
}
}
if($paging_info['curr_page'] >= $max) {
$html = $html . "<a href='1' title='Page 1'> 1</a> ... ";
}
for($i = $sp; $i <= ($sp + $max -1);$i++) {
if($i > $paging_info['pages']) { continue; }
if($paging_info['curr_page'] == $i) {
$html = $html . "<span class='bold'> {$i}</span>";
} else {
$html = $html . "<a href='{$i}' title='Page {$i}'> {$i}</a>";
}
}
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)) && ($paging_info['pages'] > ($sp + $max -1))) {
$html = $html . " ... <a href='{$paging_info['pages']}' title='Page {$paging_info['pages']}'>{$paging_info['pages']}</a>";
}
if($paging_info['curr_page'] < $paging_info['pages']) {
$html = $html . " <a href='" . ($paging_info['curr_page'] + 1) . "' title='Page " . ($paging_info['curr_page'] + 1) . "'>Next</a><a href='{$paging_info['pages']}' title='Page {$paging_info['pages']}'> Last</a>";
}
然后让代码通过数据库:
<table width="1500" border="0" cellpadding="5" cellspacing="2"
bgcolor="#666666">
<?php if($records) {?>
<tr name="headings">
<td bgcolor="#FFFFFF" name="column-heading_submission-number">Submission
Number</td>
<td bgcolor="#FFFFFF" name="column-heading_logging-server-date-&-time">Date
& Time in <?php echo $server_time ?></td>
<td bgcolor="#FFFFFF" name="column-heading_to">To</td>
<td bgcolor="#FFFFFF" name="column-heading_from">From</td>
<td bgcolor="#FFFFFF" name="column-heading_notice">Notice</td>
</tr>
<?php
$query = "SELECT id,date_and_time,recipient_username,sender_username,notice
FROM notices WHERE recipient_username = ? LIMIT ? OFFSET ?";
if ($stmt = mysqli_prepare($conn,$query))
{
mysqli_stmt_bind_param($stmt,'sii',$recipient_username, $max_results, $offset);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$id,$date_and_time,$recipient_username,$sender_username,$notice);
mysqli_stmt_store_result($stmt);
while(mysqli_stmt_fetch($stmt))
?>
<tr name="user-details">
<td bgcolor="#FFFFFF" name="submission-number"><?php echo $id; ?></td>
<td bgcolor="#FFFFFF" name="logging-server-date-&-time"><?php echo $date_and_time; ?></td>
<td bgcolor="#FFFFFF" name="recipient_username"><?php echo $recipient_username; ?></td>
<td bgcolor="#FFFFFF" name="sender_username"><?php echo $sender_username; ?></td>
<td bgcolor="#FFFFFF" name="notice"><?php echo $notice; ?></td>
</tr>
<?php
}
mysqli_stmt_close($stmt);
}
?>
<tr name="pagination"><td colspan="10" bgcolor="#FFFFFF"><?php echo $html; ?></td></tr>
注意:您显然必须编辑指向您需要它们的链接。此脚本将允许您动态设置最大页面长度,而不是将其固定为10,这可能会导致问题。