Folks,
I learnt pagination and then I wrote the first code you see below from memory (without copy & pasting its from tutorials) as a test and it works as results are shown from db.
However, few days later, I tested myself again and wrote the following 2nd code from memory and it doesn’t work. It shows: “result:0”. I get no errors.
Why? They are practically the same code these 2!
See for yourself:
1st working code
““
<?php
require ‘conn.php’;
require ‘error_reporting.php’;
?>
<!DOCTYPE HTML>
<html>
<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1.0″>
</head>
<body>
<form method=’GET’ action=”<?php echo $_SERVER[‘PHP_SELF’];?>?keywords=$keywords&limit=$limit&page=1″>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<br>
<label for=”limit”>Results per Page</label>
<select name=”limit” id=”limit”>
<option value=”1″>1</option>
<option value=”10″>10</option>
<option value=”25″>25</option>
<option value=”50″>50</option>
<option value=”100″>100</option>
</select>
<br>
<button name=”search” id=”search” value=” “>Search</button><br>
<button type=”submit” name=”search” id=”search” value=”search”>Search</button>
<br>
<input type=”reset”>
<br>
</form>
<?php
if(!ISSET($_GET[‘keywords’]))
{
die(“Type your keywords”);
}
$keywords = $_GET[‘keywords’];
//Check if the PAGE NUMBER is specified or not and if it’s a numer or not. If not, return the default: 1.
$page = ISSET($_GET[‘page’]) && is_numeric($_GET[‘page’]) ? (INT)$_GET[‘page’] : 1;
//Check if the PAGE RESULT LIMIT is specified or not and if it’s a numer or not. If not, return the default: 1.
$limit = ISSET($_GET[‘limit’]) && is_numeric($_GET[‘limit’]) ? (INT)$_GET[‘limit’] : 1;
$query_1 = “SELECT COUNT(id) FROM links WHERE keywords = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,’s’,$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
echo “ROW COUNT: $row_count<br><br>”;
}
else
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt_1));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_1));
die(“A. Prepare failed!”);
}
mysqli_stmt_close($stmt_1);
$total_pages = ceil($row_count/$limit);
htmlspecialchars($offset = (($page * $limit) – $limit));
$query_2 = “SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit”;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
{
mysqli_stmt_bind_param($stmt_2,’s’,$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
if(!$result_2)
{
die(“Fetching Error!”);
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
echo “LIMIT: $limit<br>”;
echo “ROW COUNT: $row_count<br><br>”;
echo “TOTAL PAGES: $total_pages<br><br>”;
//Retrieve Values.
$id = $row[“id”];
$page_url = $row[“page_url”];
$link_anchor_text = $row[“link_anchor_text”];
$page_description = $row[“page_description”];
$keyphrases = $row[“keyphrases”];
$keywords = $row[“keywords”];
echo “Id: $id<br>”;
echo “Page Url:”; echo urlencode(“$page_url”); echo “<br>”;
echo “Link Anchor Text: $link_anchor_text<br>”;
echo “Page Description: $page_description<br>”;
echo “Keyphrases: $keyphrases<br>”;
echo “Keywords: $keywords<br>”;
echo “<br>”;
echo “<br>”;
}
}
else
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt_2));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_2));
die(“B. Prepare failed!”);
}
mysqli_stmt_close($stmt_2);
if($page>$total_pages) //Display FINAL PAGE link.
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($total_pages) .”‘><b>Final Page</b></a>”;
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the ‘Current page’ Number.
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($i) .”‘><b>$i</b></a>”;
}
else
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($i) .”‘>$i</a>”;
}
$i++;
}
}
?>
2nd non-working code:
““
<?php
require ‘error_reporting.php’;
require ‘conn.php’;
?>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form method=”GET” action=”<?php echo $_SERVER[‘PHP_SELF’];?>?keywords=$keywords&limit=$limit&page=1″>
<label for=”keywords”>Keywords</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Type keywords”>
<br>
<label for=”limit”>Limit</label>
<select name=”limit” id=”limit”>
<option value=””></option>
<option value=”1″>1</option>
<option value=”10″>10</option>
<option value=”50″>50</option>
<option value=”100″>100</option>
<option value=”500″>500</option>
<option value=”1000″>1000</option>
</select>
<br>
<button type=”submit” name=”search” id=”search” value=” “>Search</button>
<br>
<button type=”reset”>Reset</button><br>
<br>
<br>
</form>
<?php
if(!ISSET($_GET[‘keywords’]))
{
die(“Type your Keywords!”);
}
$keywords = ISSET($_GET[‘keywords’]) && is_string($_GET[‘keywords’]);
$page = ISSET($_GET[‘page’]) && is_numeric($_GET[‘page’]) ? $_GET[‘page’] : 1;
$limit = ISSET($_GET[‘limit’]) && is_numeric($_GET[‘limit’]) ? $_GET[‘limit’] : 10;
$query_1 = “SELECT COUNT(id) FROM links WHERE keywords = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,’s’,$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
}
else
{
mysqli_stmt_close($stmt_1);
die(“A.Query Failed!”);
}
mysqli_stmt_close($stmt_1);
echo “Result: ” .$row_count .”<br>”;
$total_pages = ceil($row_count/$limit);
//$offset = htmlspecialchars($offset = (($page*$limit)-$limit));
//htmlspecialchars($offset = (($page*$limit)-$limit));
$offset = (($page*$limit)-$limit);
$query_2 = “SELECT id,domain,page_url,page_title,page_description,exclusive_offer,username FROM links WHERE keywords = ? ORDER BY id LIMIT $offset,$limit”;
$stmt_2 = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt_2,$query_2))
{
mysqli_stmt_close($stmt_2);
die(“B.Query Failed!”);
}
else
{
mysqli_stmt_bind_param($stmt_2,’s’,$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
//mysqli_stmt_fetch($stmt_2);
if(!$result_2)
{
mysqli_stmt_close($stmt_2);
die(“Fetching Error!”);
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
$id = $row[‘id’];
$domain = $row[‘domain’];
$page_url = $row[‘page_url’];
$page_title = $row[‘page_title’];
$page_description = $row[‘page_description’];
$exclusive_offer = $row[‘exclusive_offer’];
$username = $row[‘username’];
echo $id; echo “<br>”;
echo $domain; echo “<br>”;
echo urlencode($page_url); echo “<br>”;
echo $page_title; echo “<br>”;
echo $page_description; echo “<br>”;
echo $exclusive_offer; echo “<br>”;
echo $username; echo “<br>”;
}
mysqli_stmt_close($stmt_2);
}
if($page>$total_pages) //Display FINAL PAGE link.
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($total_pages) .”‘><b>Final Page</b></a>”;
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i==$page) //Bold the ‘Current page’ Number.
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($i) .”‘><b>$i</b></a>”;
}
else
{
echo “<a href='” .htmlentities($_SERVER[‘PHP_SELF’]) .’?keywords=’ .urlencode($keywords) .’&limit=’ .urlencode($limit) .’&page=’ .urlencode($i) .”‘>$i</a>”;
}
$i++;
}
}
?>
<br>
<br>
<br>
<form method=”GET” action=”<?php echo $_SERVER[‘PHP_SELF’];?>?keywords=$keywords&limit=$limit&page=1″>
<label for=”keywords”>Keywords</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Type keywords”>
<br>
<label for=”result_limit”>Limit</label>
<select name=”result_limit” id=”result_limit”>
<option value=””></option>
<option value=”1″>1</option>
<option value=”10″>10</option>
<option value=”50″>50</option>
<option value=”100″>100</option>
<option value=”500″>500</option>
<option value=”1000″>1000</option>
</select>
<br>
<button type=”submit” name=”search” id=”search” value=”submit”>Search</button>
<br>
<input type=”reset”>
<br>
<br>
<br>
</form>
</body>
</html>