/    Sign up×
Community /Pin to ProfileBookmark

Dealing With Php ceil

Folks,

I need your input here:

[code]
//$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 ?
[/code]

Context:

[code]
$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’,$_GET[‘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>”; //THIS LINE GETS ECHOED AS prepare_statement() FAILED. WHY IT FAILED ?

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 ?
[/code]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorAug 05.2020 — Folks,

Why is my prepared statement failing for no reason ? Getting on my nerves now.

In my other thread, I fixed this prolem when I removed the mysqli_stmt_close_conn($conn) from the wrong place I had put it. Then issue was solved.

Currently, in this new script, I am making not the same error and so i do not understand what is the reason this time that the prep stmt fails.

<i>
</i>$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 "&lt;br&gt;";

<i> </i> mysqli_stmt_bind_param($stmt_1,'s',$_GET['keywords']);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i>
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;"; //THIS LINE GETS TRIGGERED AND ECHOED IN TEST AS prepare_statement() FAILED. WHY IT FAILED ?
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_1));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
<i> </i> die("A. Prepare failed!");
<i> </i>}
<i> </i>
Copy linkTweet thisAlerts:
@NogDogAug 05.2020 — And what does the output of mysqli_stmt_error() say?
Copy linkTweet thisAlerts:
@developer_webauthorAug 06.2020 — @NogDog#1621826

Ooops! Sorry! I forgot to mention it.

This is what I get echoed:

**16

28

62

Error: . Error: 0. A. Prepare failed!**


The numbers are line numbers generated by: echo __LINE__;

The ELSE is getting triggered here. Not the IF:

$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',$_GET['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 62. (THIS LINE GETS TRIGGERED AND ECHOED IN TEST AS prepare_statement() FAILED. WHY IT FAILED ?).

printf("Error: %s.n", mysqli_stmt_error($stmt_1));
printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
die("A. Prepare failed!");
}
````

Read the code comment.

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorAug 06.2020 — Folks,

This is the main issue I am having.

The url is this:

http://localhost/test/pagination_test.php?

Look where the form sends you:
<i>
</i>&lt;form method='GET' action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?keywords=$keywords&amp;limit=$limit&amp;page=1"&gt;


So, clicking the SEARCH button, you are supposed to go to the same page where it's url should llok like this, if your keywords are "heman" and you select "1" on the dropdown (search results per page):

http://localhost/test/pagination_test.php?keywords=heman&limit=1&page=1

Now guess where the click sends you ? Here:

http://localhost/test/pagination_test.php?keywords=heman&limit=1&search=search

There, I get echoed:

**16

28

62

Error: . Error: 0. A. Prepare failed!**


Nums are Line Nums (echo __LINE__);


Q1. Why is the "&page=1" missing in the url ?

Q2. And, how to rid the "search=search" part ? This part not necessary as I won't be $_GETTING any parameter called "search".

Q3. Why is "&page=1" substituted by "&search=search" or why is "&page=1" truncated ?

ISSUE 2

Anyway, now I manually headover to the following url (since the SEARCH button is failing to send me there):

http://localhost/test/pagination_test.php?keywords=heman&limit=1&page=1

There, I get echoed:

**16**

Why the whole script not triggering ? Why the script stops the flow at __LINE__ 16 ? It should only stop at LINE 16 if the url contains no "keywords=". Since the url contains "keywords=heman" then the script should not have died around line 16. Look:
<i>
</i>&lt;?php
require 'conn.php';
require 'error_reporting.php';
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

echo __LINE__; echo "&lt;br&gt;"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}


Full Code, update:

Read the code comment.

Thanks!
<i>
</i>&lt;?php
require 'conn.php';
require 'error_reporting.php';
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

echo __LINE__; echo "&lt;br&gt;"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(ISSET($_GET['search']))//THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS CLICKED.
{
echo __LINE__; echo "&lt;br&gt;"; //LINE 20

<i> </i>pagination();
}

if(!ISSET($_GET['keywords'])) //THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS NOT CLICKED. UT PAGE IS LOADED VIA LINKS (PAGINATION: PAGE 2, PAGE 3, ETC.).
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}

function pagination()
{
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "&lt;br&gt;";

<i> </i> die("Type your keywords");
<i> </i>}

<i> </i>//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$limit = ISSET($_GET['limit']) &amp;&amp; is_numeric($_GET['limit']) ? $_GET['limit'] : 1;

<i> </i>//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$page = ISSET($_GET['page']) &amp;&amp; is_numeric($_GET['page']) ? $_GET['page'] : 1;

<i> </i>$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> mysqli_stmt_bind_param($stmt_1,'s',$_GET['keywords']);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i>
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;"; //LINE 62
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_1));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
<i> </i> die("A. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_1);

<i> </i>//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
<i> </i>$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
<i> </i>$offset = (($page * $limit) - $limit);

<i> </i>$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$keywords,$keywords);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i>
<i> </i> if(!$result_2)
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> die("Fetching Error");
<i> </i> }
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> echo "LIMIT: $limit&lt;br&gt;";
<i> </i> echo "ROW COUNT: $row_count&lt;br&gt;&lt;br&gt;";
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $page_url = $row["page_url"];
<i> </i> $link_anchor_text = $row["link_anchor_text"];
<i> </i> $page_description = $row["page_description"];
<i> </i> $keyphrases= $row["keyphrases"];
<i> </i> $keywords = $row["keywords"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "Page Url: $page_url&lt;br&gt;";
<i> </i> echo "Link Anchor Text: $link_anchor_text&lt;br&gt;";
<i> </i> echo "Page Description: $page_description&lt;br&gt;";
<i> </i> echo "Keyphrases: $keyphrases&lt;br&gt;";
<i> </i> echo "Keywords: $keywords&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_2));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
<i> </i> die("B. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>
<i> </i>if($page&gt;$total_pages) //If Page Numer is greater than Total Pages, show only a link to FINAL PAGE.
<i> </i>{
<i> </i> echo "?&gt;&lt;a href="pagination_test.php?limit=$limit&amp;page=$total_pages"&gt;";?&gt;&lt;?php echo "&lt;b&gt; Final Page &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page) //Bold the Current Page Number.
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> $i++;
<i> </i> }
<i> </i>}
}

function form()
{
?&gt;
&lt;form method='GET' action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?keywords=$keywords&amp;limit=$limit&amp;page=1"&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;label for="limit"&gt;Results per Page&lt;/label&gt;
&lt;select name="limit" id="limit"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="10"&gt;25&lt;/option&gt;
&lt;option value="10"&gt;25&lt;/option&gt;
&lt;option value="10"&gt;50&lt;/option&gt;
&lt;option value="10"&gt;100&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;button name="search" id="search" value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="search" id="search" value="search"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;
&lt;?php
}
?&gt;


Rearranging these lines makes no difference:
<i>
</i>if(ISSET($_GET['search']))
{
echo __LINE__; echo "&lt;br&gt;"; //THIS LINE GETS TRIGGERED AND ECHOED IN TEST

<i> </i>pagination();
}

if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}
Copy linkTweet thisAlerts:
@developer_webauthorAug 08.2020 — @NogDog,

I answered your question.

Also, I need help here:

https://www.webdeveloper.com/d/390919-why-no-row-displays

On my original post, i forgot to call the pagination(). So updated it. ut no luck.

Why is my prepared statement failing ?
<i>
</i>&lt;?php
require 'conn.php';
require 'error_reporting.php';
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

echo __LINE__; echo "&lt;br&gt;"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(ISSET($_GET['search']))//THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS CLICKED.
{
echo __LINE__; echo "&lt;br&gt;"; //LINE 20

<i> </i>pagination();
}

if(!ISSET($_GET['keywords'])) //THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS NOT CLICKED. BUT PAGE IS LOADED VIA LINKS (PAGINATION: PAGE 2, PAGE 3, ETC.).
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}

pagination();

function pagination()
{
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "&lt;br&gt;";

<i> </i> die("Type your keywords");
<i> </i>}

<i> </i>//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$limit = ISSET($_GET['limit']) &amp;&amp; is_numeric($_GET['limit']) ? $_GET['limit'] : 1;

<i> </i>//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$page = ISSET($_GET['page']) &amp;&amp; is_numeric($_GET['page']) ? $_GET['page'] : 1;

<i> </i>$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> mysqli_stmt_bind_param($stmt_1,'s',$_GET['keywords']);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i>
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;"; //LINE 62
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_1));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
<i> </i> die("A. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_1);

<i> </i>//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
<i> </i>$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
<i> </i>$offset = (($page * $limit) - $limit);

<i> </i>$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$keywords,$keywords);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i>
<i> </i> if(!$result_2)
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> die("Fetching Error");
<i> </i> }
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> echo "LIMIT: $limit&lt;br&gt;";
<i> </i> echo "ROW COUNT: $row_count&lt;br&gt;&lt;br&gt;";
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $page_url = $row["page_url"];
<i> </i> $link_anchor_text = $row["link_anchor_text"];
<i> </i> $page_description = $row["page_description"];
<i> </i> $keyphrases= $row["keyphrases"];
<i> </i> $keywords = $row["keywords"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "Page Url: $page_url&lt;br&gt;";
<i> </i> echo "Link Anchor Text: $link_anchor_text&lt;br&gt;";
<i> </i> echo "Page Description: $page_description&lt;br&gt;";
<i> </i> echo "Keyphrases: $keyphrases&lt;br&gt;";
<i> </i> echo "Keywords: $keywords&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_2));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
<i> </i> die("B. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>
<i> </i>if($page&gt;$total_pages) //If Page Numer is greater than Total Pages, show only a link to FINAL PAGE.
<i> </i>{
<i> </i> echo "?&gt;&lt;a href="pagination_test.php?limit=$limit&amp;page=$total_pages"&gt;";?&gt;&lt;?php echo "&lt;b&gt; Final Page &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page) //Bold the Current Page Number.
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> $i++;
<i> </i> }
<i> </i>}
}

function form()
{
?&gt;
&lt;form method='GET' action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?keywords=$keywords&amp;limit=$limit&amp;page=1"&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;label for="limit"&gt;Results per Page&lt;/label&gt;
&lt;select name="limit" id="limit"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="10"&gt;25&lt;/option&gt;
&lt;option value="10"&gt;25&lt;/option&gt;
&lt;option value="10"&gt;50&lt;/option&gt;
&lt;option value="10"&gt;100&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;button name="search" id="search" value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="search" id="search" value="search"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;
&lt;?php
}
?&gt;
Copy linkTweet thisAlerts:
@developer_webauthorAug 08.2020 — UPDATE:

I had error on error_reporting.php.

Here:
<i>
</i>ini_set('error_reporting','E_ALL');//error_reporting(E_ALL);


Fixed it:
<i>
</i>ini_set('error_reporting',E_ALL);//error_reporting(E_ALL);


I now get error:

Notice: Undefined variable: conn in C:xampphtdocstestpagination_test.php on line 51

Notice: $conn is defined in conn.php (required file):

pagination_test.php

<i>
</i>&lt;?php
require 'conn.php';
require 'error_reporting.php';
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

echo __LINE__; echo "&lt;br&gt;"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(ISSET($_GET['search']))//THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS CLICKED.
{
echo __LINE__; echo "&lt;br&gt;"; //LINE 20

<i> </i>pagination();
}

if(!ISSET($_GET['keywords'])) //THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS NOT CLICKED. BUT PAGE IS LOADED VIA LINKS (PAGINATION: PAGE 2, PAGE 3, ETC.).
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}

pagination();

function pagination()
{
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "&lt;br&gt;";

<i> </i> die("Type your keywords");
<i> </i>}

<i> </i>//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$limit = ISSET($_GET['limit']) &amp;&amp; is_numeric($_GET['limit']) ? $_GET['limit'] : 1;

<i> </i>//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$page = ISSET($_GET['page']) &amp;&amp; is_numeric($_GET['page']) ? $_GET['page'] : 1;

<i> </i>$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> mysqli_stmt_bind_param($stmt_1,'s',$_GET['keywords']);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i>
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;"; //LINE 64
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_1));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
<i> </i> die("A. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_1);

<i> </i>//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
<i> </i>$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
<i> </i>$offset = (($page * $limit) - $limit);

<i> </i>$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{
<i> </i> mysqli_stmt_bind_param($stmt_2,"s",$keywords);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i>
<i> </i> if(!$result_2)
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> die("Fetching Error");
<i> </i> }
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> echo "LIMIT: $limit&lt;br&gt;";
<i> </i> echo "ROW COUNT: $row_count&lt;br&gt;&lt;br&gt;";
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $page_url = $row["page_url"];
<i> </i> $link_anchor_text = $row["link_anchor_text"];
<i> </i> $page_description = $row["page_description"];
<i> </i> $keyphrases= $row["keyphrases"];
<i> </i> $keywords = $row["keywords"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "Page Url: $page_url&lt;br&gt;";
<i> </i> echo "Link Anchor Text: $link_anchor_text&lt;br&gt;";
<i> </i> echo "Page Description: $page_description&lt;br&gt;";
<i> </i> echo "Keyphrases: $keyphrases&lt;br&gt;";
<i> </i> echo "Keywords: $keywords&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_2));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
<i> </i> die("B. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>
<i> </i>if($page&gt;$total_pages) //If Page Numer is greater than Total Pages, show only a link to FINAL PAGE.
<i> </i>{
<i> </i> echo "?&gt;&lt;a href="pagination_test.php?limit=$limit&amp;page=$total_pages"&gt;";?&gt;&lt;?php echo "&lt;b&gt; Final Page &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page) //Bold the Current Page Number.
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> $i++;
<i> </i> }
<i> </i>}
}

function form()
{
?&gt;
&lt;form method='GET' action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?keywords=$keywords&amp;limit=$limit&amp;page=1"&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;label for="limit"&gt;Results per Page&lt;/label&gt;
&lt;select name="limit" id="limit"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="10"&gt;10&lt;/option&gt;
&lt;option value="25"&gt;25&lt;/option&gt;
&lt;option value="50"&gt;50&lt;/option&gt;
&lt;option value="100"&gt;100&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;button name="search" id="search" value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="search" id="search" value="search"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;
&lt;?php
}
?&gt;


Do I need to add require 'conn.php'; again inside the pagination() function ?

Might aswell add it then .....

pagination_test.php
<i>
</i>&lt;?php
require 'conn.php';
require 'error_reporting.php';
?&gt;

&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

//SCRIPT FLOW STARTS HERE.

echo __LINE__; echo "&lt;br&gt;"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST

if(ISSET($_GET['search']))//THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS CLICKED.
{
echo __LINE__; echo "&lt;br&gt;"; //LINE 20

<i> </i>pagination();
}

if(!ISSET($_GET['keywords'])) //THIS LINE WILL TRIGGER WHEN 'SEARCH' BUTTON IS NOT CLICKED. BUT PAGE IS LOADED VIA LINKS (PAGINATION: PAGE 2, PAGE 3, ETC.).
{
echo __LINE__; echo "&lt;br&gt;"; echo "&lt;br&gt;";

<i> </i>form();
<i> </i>die;
}

pagination();

//SCRIPT FLOW ENDS HERE.

//ONLY FUNCTION DEFINITIONS FROM THIS POINT ONWARDS
function pagination()
{
require 'conn.php';//Re-added this inside the pagination() function this time as it gives error "undefined $conn" without re-addition.
require 'error_reporting.php'; //Re-added this inside the pagination() function too.

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

<i> </i>//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$limit = ISSET($_GET['limit']) &amp;&amp; is_numeric($_GET['limit']) ? $_GET['limit'] : 1;

<i> </i>//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
<i> </i>$page = ISSET($_GET['page']) &amp;&amp; is_numeric($_GET['page']) ? $_GET['page'] : 1;

<i> </i>$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> mysqli_stmt_bind_param($stmt_1,'s',$_GET['keywords']);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i>
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;"; //LINE 64
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_1));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_1));
<i> </i> die("A. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_1);

<i> </i>//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
<i> </i>$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
<i> </i>$offset = (($page * $limit) - $limit);

<i> </i>$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{
<i> </i> mysqli_stmt_bind_param($stmt_2,'s',$keywords);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i>
<i> </i> if(!$result_2)
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> die("Fetching Error");
<i> </i> }
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> echo "LIMIT: $limit&lt;br&gt;";
<i> </i> echo "ROW COUNT: $row_count&lt;br&gt;&lt;br&gt;";
<i> </i> echo "TOTAL PAGES: $total_pages&lt;br&gt;&lt;br&gt;";
<i> </i>
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $page_url = $row["page_url"];
<i> </i> $link_anchor_text = $row["link_anchor_text"];
<i> </i> $page_description = $row["page_description"];
<i> </i> $keyphrases= $row["keyphrases"];
<i> </i> $keywords = $row["keywords"];
<i> </i>
<i> </i> echo "Id: $id&lt;br&gt;";
<i> </i> echo "Page Url: $page_url&lt;br&gt;";
<i> </i> echo "Link Anchor Text: $link_anchor_text&lt;br&gt;";
<i> </i> echo "Page Description: $page_description&lt;br&gt;";
<i> </i> echo "Keyphrases: $keyphrases&lt;br&gt;";
<i> </i> echo "Keywords: $keywords&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> echo __LINE__; echo "&lt;br&gt;";
<i> </i>
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt_2));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt_2));
<i> </i> die("B. Prepare failed!");
<i> </i>}

<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>
<i> </i>if($page&gt;$total_pages) //If Page Numer is greater than Total Pages, show only a link to FINAL PAGE.
<i> </i>{
<i> </i> echo "?&gt;&lt;a href="pagination_test.php?limit=$limit&amp;page=$total_pages"&gt;";?&gt;&lt;?php echo "&lt;b&gt; Final Page &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> $i = 1;
<i> </i> while($i&lt;=$total_pages)
<i> </i> {
<i> </i> if($i&lt;$total_pages)
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo " $i ";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> elseif($i==$page) //Bold the Current Page Number.
<i> </i> {
<i> </i> echo "&lt;a href="pagination_test.php?keywords=$keywords&amp;limit=$limit&amp;page=$i"&gt;";?&gt;&lt;?php echo "&lt;b&gt; $i &lt;/b&gt;";?&gt;&lt;/a&gt;&lt;?php
<i> </i> }
<i> </i> $i++;
<i> </i> }
<i> </i>}
}

function form()
{
?&gt;
&lt;form method='GET' action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?keywords=$keywords&amp;limit=$limit&amp;page=1"&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;label for="limit"&gt;Results per Page&lt;/label&gt;
&lt;select name="limit" id="limit"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="10"&gt;10&lt;/option&gt;
&lt;option value="25"&gt;25&lt;/option&gt;
&lt;option value="50"&gt;50&lt;/option&gt;
&lt;option value="100"&gt;100&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;button name="search" id="search" value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="search" id="search" value="search"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;
&lt;?php
}
?&gt;


LATEST TEST RESULT:

I get echoed no results from mysql tl.

I get this echoed on screen:

18

22

62

1 62

1

These are line numbers due to echo '__LINE__'.

The "1" are links. Pagination links. Pagination "1" should not echo twice. new issue.

I searched for keyword "heman". entries exist and so PAGE 1 should show the first entry (as one entry/row per page based on $limit=1) and the nd entry/row should e displayed on PAGE 2. So, pagination section should display page numbers like so:

12

And not like this nonsense:

1 62

1

(62 is a line echo via echo __LINE__);

So currently the two issues are:

  • 1. Tbl rows not get displayed.

  • 2. Pagination Link "1" gets echoed twice.

  • 3. Pagination numbers do not properly display.
  • Copy linkTweet thisAlerts:
    @developer_webauthorAug 24.2020 — No one knows the answer ?
    Copy linkTweet thisAlerts:
    @NogDogAug 25.2020 — > @developer_web#1622491 No one knows the answer ?

    More likely no one has the time to read through all that code. I surely do not.
    Copy linkTweet thisAlerts:
    @shobujkumerAug 25.2020 — #BuyUAEFaxList For a significant period of time an indispensable bit of Spanish Culture has been the 'Gathering,' and maybe the best ways to deal with totally douse your self in boa fife Spanish show is to visit one of these striking, vigorous celebrations of life.Sometimes it seems like festivals run progressively from one into the accompanying, the Spanish genuinely love to celebrate. Here is a manual for promise you don't miss the best and the best festivals in Murcia. For insistence of dates and posting of what may be happening it's optimal to contact Murcia's Tourist Information. Nuances of which can be found at the base of this page.

    The Spring Festival in Murcia is held commonly quickly after Easter, it's an open entryway for the total of the enveloping towns to come and dress in their close by neighborhood weaved skirts and Dardanelles pants. It's a festival to propel the whole of the market garden workers, such an agribusiness stand-out to this area of Spain. Like an introduction between male peacocks the towns put vigorously in their appearance and battle in individuals moving, amazingly concealed floats upgrade the paths and each open town square transforms into a cause move to promote selling delightful traditional dishes of Murcia, make sure to endeavor the "chalets DE recorder AL ado mystery" A dish of flavorful sheep cuts served in a stand-out garlic vinegar dressing.

    The made sure about woodland of this bit of Spain is loaded down with close by game. If you get the open entryway gorge your self on the grilled rabbit, bunny, partridge and quail.

    The fundamental day of cheerfulness start with an endowment of Virgin of Fuentes, the supporter of the city. From here splendid floats travel around the city. My suggestion is to wind up at one of the various mind blowing tapas bars on the way, welcome a glass of the area Camilla region wine and let the celebration condition come to you.

    [url=https://www.faxlistb.com/uae-fax-broadcast-list/]Buy UAE Fax List]



    The following Saturday the celebrations take an unusual twist. Possibly the most extraordinary cheer of the year everything incorporates the devouring of a sardine resemblance and engravings the lamenting and end of the festivity.Brass gatherings, support walkers, Chinese legendary mammoths, specialists and strolling downturns trim the city roads to the sound of Spanish music. Celebrating goes on late into the night so make sure to take your best flamenco moving shoes. During September Murcia awakens again to commend the City's advocate sacred individual La Virgin DE la Fuentes, walks, old stories and shows paint the perfect setting for this Autumnal festival.



    A couple of more diminutive festivals, for instance, the famous music, film, bullfighting and Mediterranean old stories festivities are participate to the essential festival, and occur in disengaged territories. Make sure to scourer the traveler information concentrate so you don't miss anything. There are even execution pieces from Marcia's twin town of Lodz in Poland, the atmosphere makes sure to be electric in this extremely all inclusive festival. From the End of January for two months a City wide type of the Tate Modern occurs.



    Encouraged in the place of petition, spots of love, existing displays, and a couple of the outside squares in Murcia it's an irrefutable necessity see for any workmanship present day craftsmanship fans. Do whatever it takes not to freeze you don't have to wear a certifiable search for a day and dress only in dim while you endeavor to comprehend what the authorities are trying to bestow, there are guided visits that will talk you through created by close by and all inclusive skilled workers. With a sort of our Turner prize want to see a wide scope of stunning hypothetical craftsmanship.



    A years prior event indicated a piece by Laurent Adoration and Abraham Chevalier it was a more prominent measure of an encounter than a magnum opus. They tunneled an underground section by hand of some place in the scope of twenty meters and filled them selves in as they went. It addressed a hypothetical 'underground trailer'. With living space only several meters in length and 166 cm high the two made due in the space for an extensive time allotment. They were given common air during the day unearthing and were simply in contact with the outside world by methods for radios. The best way to deal with test these joy is point of fact with the usage of a select vehicle. On the off chance that you're settled not to miss anything and feeling to some degree more gutsy on your excursion you ought to consider utilizing.
    Copy linkTweet thisAlerts:
    @developer_webauthorAug 26.2020 — @NogDog#1622500

    Ok. How-about answering this particular post then ?

    https://www.webdeveloper.com/d/390993-why-the-condition-fails/5
    Copy linkTweet thisAlerts:
    @hemangjoshi37aAug 31.2020 — You should keep the below one line because it is more correct.


    Hemang Joshi,

    Web Designer,
    Copy linkTweet thisAlerts:
    @developer_webauthorAug 31.2020 — @Sempervivum,

    After answering this post:

    https://www.webdeveloper.com/d/390902-dealing-with-php-ceil/7

    Do close this thread.

    Thank You!
    ×

    Success!

    Help @developer_web 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,
    )...