/    Sign up×
Community /Pin to ProfileBookmark

Why Mysql Fails To Spit Tbl Data ?

Folkies,

I built a php pagination using mysqli in procedural style programming.
Event hough my tbl has 2 entries, the pagination page is not showing it. Showing 0 results found message.
Care to have a look at my complete code to figure-out where I am going wrong. Put your attention on the 2 queries. I have a feeling the problem may lie there. I do have error reporting on on one of the inlcuded files. Getting no errors.

[code]
<?php
//Required PHP Files.
include ‘header_account.php’; //Required on all webpages of the Site.
?>

<?php

if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
exit();
}
else
{
//Get the Page Number. Default is 1 (First Page).
$page_number = $_GET[“page_number”];
if ($page_number == “”)
{
$page_number = 1;
}

$sender_username = $social_network_admin_username;
$recipient_username = $user;
$links_per_page = 1;
$max_result = 100;
//$offset = ($page_number*$links_per_page)-$links_per_page;
$offset = ($page_number-1)*$links_per_page;

//$query_1 = “SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ?”;
$query_1 = “SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?”;
$stmt_1 = mysqli_prepare($conn,$query_1);
//mysqli_stmt_bind_param($stmt_1,’ss’,$recipient_username,$sender_username);
mysqli_stmt_bind_param($stmt_1,’ssii’,$recipient_username,$sender_username,$links_per_page,$offset);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$matching_rows_count);
mysqli_stmt_fetch($stmt_1);
mysqli_stmt_free_result($stmt_1);

$total_pages = ceil($matching_rows_count/$links_per_page);
$query_2 = “SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?”;
$stmt_2 = mysqli_prepare($conn,$query_2);
mysqli_stmt_bind_param($stmt_2,’ssii’,$recipient_username,$sender_username,$links_per_page,$offset);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$recipient_username,$sender_username,$notice);
mysqli_stmt_fetch($stmt_2);
?>
<!DOCTYPE html>
<html>
<head>
<meta content=”text/html; charset=ISO-8859-1″ http-equiv=” content-type”>
<title><?php echo “$site_name User $user Notices in $server_time time.”; ?></title>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
</head>
<body>
<br>
<p align=”center”><span style=”font-weight:bold;”><?php echo “$site_name User $user Notices in $server_time time.”; ?></span></align>
<br>
<br>
<table width=”1500″ border=”0″ cellpadding=”5″ cellspacing=”2″ bgcolor=”#666666″>
<?php if(!$stmt_2)
{
?>
<tr>
<td bgcolor=”#FFFFFF”>No record found! Try another time.</td>
</tr>
<?php
}
else
{
if(($offset+1)<=$max_result)
{
printf(“<b> %d Result Found …</b>n”,$matching_rows_count); ?><br>
<br>
<tr name=”headings”>
<td bgcolor=”#FFFFFF” name=”column-heading_submission-number”>Submission Number</td>
<td bgcolor=”#FFFFFF” name=”column-heading_logging-server-date-and-time”>Date & Time in <?php echo “$server_time” ?></td>
<td bgcolor=”#FFFFFF” name=”column-heading_recipient-username”>To</td>
<td bgcolor=”#FFFFFF” name=”column-heading_sender-username”>From</td>
<td bgcolor=”#FFFFFF” name=”column-heading_notice”>Notice</td>
</tr>
<tr name=”user-details”>
<td bgcolor=”#FFFFFF” name=”submission-number”><?php printf(“%s”,$id); ?></td>
<td bgcolor=”#FFFFFF” name=”logging-server-date-and-time”><?php printf(“%s”,$date_and_time); ?></td>
<td bgcolor=”FFFFFF” name=”column-heading_recipient-username”><?php printf(“%s”,$recipient_username); ?></td>
<td bgcolor=”#FFFFFF” name=”column-heading_sender-username”><?php printf(“%s”,$sender_username); ?></td>
<td bgcolor=”#FFFFFF” name=”notice”><?php printf(“%s”,$notice); ?></td>
</tr>
<?php
//Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php
while(mysqli_stmt_fetch($stmt_2))
{
?>
<tr name=”user-details”>
<td bgcolor=”#FFFFFF” name=”submission-number”><?php printf(“%s”,$id); ?></td>
<td bgcolor=”#FFFFFF” name=”logging-server-date-and-time”><?php printf(“%s”,$date_and_time); ?></td>
<td bgcolor=”#FFFFFF” name=”recipient-username”><?php printf(“%s”,$recipient_username); ?></td>
<td bgcolor=”#FFFFFF” name=”sender-username”><?php printf(“%s”,$sender_username); ?></td>
<td bgcolor=”#FFFFFF” name=”notice”><?php printf(“%s”,$notice); ?></td>
</tr>
<?php
}
?>
<tr name=”pagination”>
<td colspan=”10″ bgcolor=”#FFFFFF”> Result Pages:
<?php
if($page_number<$total_pages)
{
for($i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
echo “<a href=”{$_SERVER[‘PHP_SELF’]}?user=$user&page_number={$i}”>{$i}</a> “;
?><br>
<?php echo “$total_pages”; //DELETE
}
else
{
for($i=$total_pages;$i>=1;$i–) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
echo “<a href=”{$_SERVER[‘PHP_SELF’]}?user=$user&page_number={$i}”>{$i}</a> “;
?><br>
<?php echo “$total_pages”; //DELETE
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
<br>
<br>
<p align=”center”><span style=”font-weight:bold;”><?php echo “$site_name User $user Notices in $server_time time.”; ?></span></align>
<br>
</div>
<br>
</body>
</html>
<?php
//Free Result Set.
mysqli_stmt_free_result($stmt_2);
//Close Statement Connection.
mysqli_stmt_close($stmt_2);
//Close Database Connection.
mysqli_close($conn);
}
?>

<?php
include ‘footer_account.php’; //Required on all webpages of the Site.
?>

[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...