-1

Folks,

I am not killing the script flow, before the CONDITION, with

die();

And so, one of the 2 should trigger: IF ELSE

But they don't.

if(!$result_2)
    {
        echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
        
        die("Fetching Error");
    }
    while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
    {
        echo __LINE__; echo "<br>";//LINE 98: THIS LINE IS LIKE AN 'ELSE'. IT  DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
        

Either Line 92 (IF) should trigger or Line 98 (which is like the ELSE).

Do notice the comments in the code to understand where the script is ending the flow without any reason.

<?php

$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))
{
    echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. LAST LINE THAT GETS ECHOED.
    
    mysqli_stmt_bind_param($stmt_2,'s',$keywords);
    mysqli_stmt_execute($stmt_2);           
    $result_2 = mysqli_stmt_get_result($stmt_2);
    
    if(!$result_2)
    {
        echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'ELSE' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
        
        die("Fetching Error");
    }

What is killing the script flow ?

Context:

<?php

echo __LINE__; echo "<br>"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(!ISSET($_GET['keywords']))
{
    echo __LINE__; echo "<br>";
    
    die("Type your keywords");
}

$keyword = $_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']) ? $_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']) ? $_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))
{
    echo __LINE__; echo "<br>";//LINE 57: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. BUT WHY SCRIPT FLOW DOES NOT GO BEYOND THIS LINE ?
    
    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
{
    echo __LINE__; echo "<br>"; //LINE 67
    
    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($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
$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))
{
    echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. LAST LINE THAT GETS ECHOED.
    
    mysqli_stmt_bind_param($stmt_2,'s',$keywords);
    mysqli_stmt_execute($stmt_2);           
    $result_2 = mysqli_stmt_get_result($stmt_2);
    
    if(!$result_2)
    {
        echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
        
        die("Fetching Error");
    }
    while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
    {
        echo __LINE__; echo "<br>";//LINE 98: THIS LINE IS LIKE AN  'ELSE'. IT DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
        
        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: $page_url<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
{
    echo __LINE__; echo "<br>";//Line 124
    
    printf("Error: %s.\n", mysqli_stmt_error($stmt_2));
    printf("Error: %d.\n", mysqli_stmt_errno($stmt_2));
    die("B. Prepare failed!");
}

Also, out of these 2, which one is correct ?

$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?

UPDATE My code now looks like this. My issues are mentioned clearly in the code comments in CAPITALS.

CURRENT ISSUES:

ISSUE 1: Pagination Section only displays link to PAGE 1 from PAGE 1. Fails to display link to PAGE 2. Code configured to display 1 row per page. Since there are 2 matching rows then naturally 2 pages should display all the results. Why pagination section fails to display link to PAGE 2 from PAGE 1 ?

error_reporting.php

<?php

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
ini_set('error_reporting',E_ALL);
?>
<?php
require 'conn.php';
require 'error_reporting.php';
?>

<!DOCTYPE HTML">
<html>

<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</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

echo __LINE__; echo "<br>"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(!ISSET($_GET['keywords']))
{
    echo __LINE__; echo "<br>";
    
    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']) ? $_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']) ? $_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))
{
    echo __LINE__; echo "<br>";//LINE 57: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. 
    
    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
{
    echo __LINE__; echo "<br>"; //LINE 67
    
    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);
$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)) //LINE 82: 
{
    echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND SO PREPARED STATEMENT DID NOT FAIL AT LINE 82!
    
    mysqli_stmt_bind_param($stmt_2,'s',$keywords);
    mysqli_stmt_execute($stmt_2);           
    $result_2 = mysqli_stmt_get_result($stmt_2);
    
    if(!$result_2)
    {
        echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
        
        die("Fetching Error");
    }
    while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))//LINE 96: THIS LINE IS LIKE AN 'ELSE'. IT DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
    {
        echo __LINE__; echo "<br>";
        
        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: $page_url<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
{
    echo __LINE__; echo "<br>";//LINE 124
    
    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);

//PAGINATION SECTION STARTS FROM HERE:
//WHY PAGINATION SECTION FAILS TO DISPLAY ? ONLY A LINK TO PAGE 1 IS SHOWN WHEN YOU LOAD PAGE 1. NO LINK TO PAGE 2. THERE ARE 2 MATCHING ROWS. SCRIPT FIXED TO SHOW 1 ROW PER PAGE. AND SO, 2 PAGES SHOULD DISPLAY ALL RESULTS. HENCE, PAGINATION SECTION SHOULD SHOW A LINK TO PAGE 2 FROM PAGE 1. BUT PAGE 1 ONLY LINKS TO ITSELF IN THE PAGINATION SECTION.
if($page>$total_pages) //If Page Number is greater than Total Pages, show only a link to FINAL PAGE.
{
    echo "?><a href=\"pagination_test.php?limit=$limit&page=$total_pages\">";?><?php echo "<b> Final Page </b>";?></a><?php 
}
else
{
    $i = 1;
    while($i<=$total_pages)
    {
        if($i<$total_pages)
        {
            echo "<a href=\"pagination_test.php?keywords=$keywords&limit=$limit&page=$i\">";?><?php echo " $i ";?></a><?php 
        }
        elseif($i==$page) //Bold the Current Page Number.
        {
            echo "<a href=\"pagination_test.php?keywords=$keywords&limit=$limit&page=$i\">";?><?php echo "<b> $i </b>";?></a><?php 
        }               
        $i++;
    }
}
?>

UPDATE AGAIN: ISSUE: Missing in Url "&page=".

Look at this html:

<form method='GET' action="<?php echo $_SERVER['PHP_SELF'];?>?keywords=$keywords&limit=$limit&page=1">

As you can see, after clicking the form button, you should be sent to: pagination_test_2.php?keywords=$keywords&limit=$limit&page=1"

But guess what ? you are sent to: ?keywords=$keywords&limit=$limit&search=search"

Where is the "&page=1" part in the url ? Also, how not to have "search=search" added to the url ?

<?php
require 'conn.php';
require 'error_reporting.php';
?>

<!DOCTYPE HTML">
<html>

<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</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

echo __LINE__; echo "<br>";

if(!ISSET($_GET['keywords']))
{
    echo __LINE__; echo "<br>";
    
    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']) ? $_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']) ? $_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))
{
    echo __LINE__; echo "<br>";
    
    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
{
    echo __LINE__; echo "<br>";
    
    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);
$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))
{
    echo __LINE__; echo "<br>";
    
    mysqli_stmt_bind_param($stmt_2,'s',$keywords);
    mysqli_stmt_execute($stmt_2);           
    $result_2 = mysqli_stmt_get_result($stmt_2);
        
    if(!$result_2)
    {
        echo __LINE__; echo "<br>";
        
        die("Fetching Error");
    }
    
    while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
    {
        echo __LINE__; echo "<br>";
        
        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: $page_url<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
{
    echo __LINE__; echo "<br>";
    
    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) //If Page Number is greater than Total Pages, show only a link to FINAL PAGE.
{
    echo "<a href=\"pagination_test.php?keywords=$keywords&limit=$limit&page=$total_pages\">"; echo "<b> Final Page </b>";?></a><?php
}
else
{
    $i = 1;
    while($i<=$total_pages)
    {
        if($i<$total_pages && $i!=$page)
        {
            echo "<a href=\"pagination_test_2.php?keywords=$keywords&limit=$limit&page=$i\">"; echo " $i ";?></a><?php
        }
        elseif($i==$page) //Bold the Current Page Number.
        {
            echo "<a href=\"pagination_test_2.php?keywords=$keywords&limit=$limit&page=$i\">"; echo "<b> $i </b>";?></a><?php
        }
        else
        {
            echo "<a href=\"pagination_test_2.php?keywords=$keywords&limit=$limit&page=$i\">"; echo " $i ";?></a><?php
        }
        $i++;
    }
}
?>

Typos were on the urls in the pagination section. Now, I fixed them and so pagination section working fine. Can someone be kind enough to checkout my code and let me know which lines to weedout if they are not really necessary.

  • 3
    Line 98 is NOT like ELSE. It won't trigger if your query returned no rows. – Your Common Sense Aug 14 '20 at 11:34
  • 3
    Your persistence in avoiding best practices is disheartening. – Your Common Sense Aug 14 '20 at 11:34
  • @YCS, I do not understand. This is how I see it: If there are no results then line 92 should echo and die() the script flow there. However, if there are results (rows to return) then the WHILE loop should trigger and line 98 should echo. Is this not how it is supposed to be or I understood things wrong ? Because, that is how I programmed the script flow. – power.page Aug 14 '20 at 11:43
  • @YCS, What best practices are you talking about ? Still a beginner. Not intermediate yet. Can get to deeper level when intermediate. Got to walk before I can run or I will trip over, mate. Still a toddler in php. – power.page Aug 14 '20 at 11:45
  • If there is no money in your wallet, does it mean you don't have a wallet as well? Or may be there are things that exist themselves but Don't contain anything inside? – Your Common Sense Aug 14 '20 at 11:46
  • Nobody's asking you to win Olympic games. Just the proper synax, introduced to you many times already that will let you to get rid of ALL those conditions – Your Common Sense Aug 14 '20 at 11:48
  • Just a hunch. You are not getting error, but your triggers are not working. I think the reason is that you are making `$keyword = $_GET['keywords'];`, variable without `s` but using `mysqli_stmt_bind_param($stmt_2,'s',$keywords);` making your query to look for records `FROM links WHERE keywords =''`. Also you should be getting `Undefined Variable` warning in your error logs. – Umair Khan Aug 14 '20 at 11:51
  • The last time ever I am [rewriting your code to proper PHP](https://3v4l.org/dhL5f). given you are [connecting with mysqli properly](https://phpdelusions.net/mysqli/mysqli_connect) as well, this ls **all** the mysql related code you need. – Your Common Sense Aug 14 '20 at 12:57
  • @Umair Khan, You are correct about my typo. I fixed it. Funny thing is, I did not get any UNDEFINED variable error! – power.page Aug 17 '20 at 12:47
  • @YMC, Sorry mate. I did tell you before in one of my other threads that I do not quite understand your code in your following link because it is in oop and I only know procedural. https://phpdelusions.net/mysqli/mysqli_connect Ofcourse, you are welcome to add procedural style on your link for me to learn it from. – power.page Aug 17 '20 at 12:49
  • @power.page So the original error was a typo? Also please see this for [php error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Umair Khan Aug 17 '20 at 12:50
  • @Umair Khan, I already got error reporting on: require 'error_reporting.php'; Check my origional post for section UPDATE. – power.page Aug 17 '20 at 14:42

1 Answers1

0

I think neither lines are reached because the statement is valid but returns zero rows. Before $query_2 = ... could you add a check on $row_count to see if there are any rows?

To answer your second question, $total_pages = ceil($row_count/$limit); is the correct one to use because mysqli_stmt_bind_result returns a boolean so $result_1 would be true or false.

Personally I prefer object oriented style rather than procedural style as you have used because I find it easier to read but the choice is yours.

Mark
  • 1,006
  • 1
  • 6
  • 6
  • @YMC, I tested your script: https://3v4l.org/dhL5f It is failing to display any results. Neither it displays any results, even though 2 matching rows exist for my searched keyword. Nor does it echo "No data found for such a keyword". Strange! I do not see any error in your coding, even though I do not know oop, I can understand it a little comparing it with oop pagination tutorials found online. My mission is to build pagination procedural style since I understand that style. – power.page Aug 17 '20 at 13:06
  • Mark, sorry for the late reply. I think you are correct. After $query_1, I echoed $row_count and nothing gets echoed. After $query_2, I echoed $row_count and it gets echoed "2" as there are 2 row matches. I spotted that, I had not mysqli_stmt_fetch() before echoing $row_count after query_1. Fixed it. – power.page Aug 17 '20 at 14:36
  • I fixed my pagination. But small issue in the url where form submission sends you. Do check my original post at the end under the heading: Missing in Url "&page=". – power.page Aug 17 '20 at 15:18
  • @YMC, It would help me to understand your link if your code was in procedural style and so do you mind adding procedural style to your link which you mentioned ? – power.page Aug 17 '20 at 15:19
  • @Umair Khan, More typos were in the urls on the pagination section. Fixed them and pagination section now working fine. I am adding my latest update in my original post. At the end. – power.page Aug 17 '20 at 15:19