/    Sign up×
Community /Pin to ProfileBookmark

Why One Works While The Other Doesn’t ?>

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>
““

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 15.2020 — > @developer_web#1623317 They are practically the same code

Okay, so what's different? (I'm not going to read through a few hundred lines of code to figure that out.)

PS: If you put things into narrowly scoped functions, you can re-use those functions in multiple places and multiple projects, rather than rewriting them from scratch each time and hoping to get the same results.
Copy linkTweet thisAlerts:
@VITSUSASep 16.2020 — @developer_web#1623317 I agree with NogDog, people have no time to read a few hundred lines of code to figure that out. In this situation you should define the function at once after that you can re-use those functions in multiple places and multiple projects, rather than rewriting them from scratch each time.
Copy linkTweet thisAlerts:
@developer_webauthorSep 25.2020 — @NogDog#1623318

Sorry for late reply. Was ill.

Only difference, 1st one:

IF->MYSQLI_PREPARE ->Continue Script Flow;

Else->Show Error;

2nd: one:

IF->NOT->MYSQLI_PREPARE->Show Error;

Else->Continue Script Flow;

I see no other difference.

You are welcome to check the 2nd scipt that is not working to see where I went wrong. I do not spot my mistake.

1st one context:

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))
{


2nd one context:

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))
{
Copy linkTweet thisAlerts:
@NogDogSep 25.2020 — No idea. I'd suggest seeing if you can get more debug info into your die() statements so you can see _why_ it died.
<i>
</i>die("&lt;pre&gt;B.Query Failed!n".print_r(mysqli_error_list($conn))."&lt;/pre&gt;");

No guarantee that syntax is correct since I have no way to test it.

PS: that's just temporary debug code, you do not want it on a live site.
Copy linkTweet thisAlerts:
@developer_webauthorSep 27.2020 — @NogDog#1623670

It's not dieing.

I only get echoed:

**Result: 0**
Copy linkTweet thisAlerts:
@developer_webauthorSep 27.2020 — @NogDog,

After further tests, I now conclude prep not going ahead. But why ?


if(!mysqli_stmt_prepare($stmt_2,$query_2))


Context:

$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!");
//die("<pre>B.Query Failed!n".print_r(mysqli_error_list($conn))."</pre>");
}
Copy linkTweet thisAlerts:
@developer_webauthorSep 27.2020 — @NogDog,

Working now!

I just switched this:


$keywords = ISSET($_GET['keywords']) && is_string($_GET['keywords']);


to:


$keywords = $_GET['keywords'];


Why this caused disruption ?

is_string
Copy linkTweet thisAlerts:
@NogDogSep 27.2020 — > @developer_web#1623695 $keywords = ISSET($_GET['keywords']) &amp;&amp; is_string($_GET['keywords']);

That would set $keywords to a Boolean true/false.
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — @NogDog#1623700

Exactly,

So, if $_GET['keywords'] is boolean TRUE to be a string, then $keywords should be set. Else, not get set and $keywords should be empty.

This:


$keywords = ISSET($_GET['keywords']) && is_string($_GET['keywords']);


as good as this:


if(ISSET($_GET['keywords']) && is_string($_GET['keywords']);)
{
$keywords = $_GET['keywords'];
}
else
{
die("Type your keywords");
}


or as good as this:


if(!ISSET($_GET['keywords']) || !is_string($_GET['keywords']);)
{
die("Type your keywords");
}
else
{
$keywords = $_GET['keywords'];
}


Hence, $keywords should have been set. Right ? But it stayed empty!
Copy linkTweet thisAlerts:
@NachfolgerOct 04.2020 — > @developer_web#1623883 So, if $_GET['keywords'] is boolean TRUE to be a string, then $keywords should be set. Else, not get set and $keywords should be empty.

That's wrong. Completely and utterly incorrect. Run your code in a simulation to figure out why it's wrong, and look at the proper format of a ternary statement--Which is what you're trying to accomplish here.

I will say this:
``PHP<i>
</i>$keywords = ISSET($_GET['keywords']) &amp;&amp; is_string($_GET['keywords']);<i>
</i>
``

Is ALMOST what you want, but it's missing two (critical) portions of a ternary statement.
Copy linkTweet thisAlerts:
@developer_webauthorOct 07.2020 — @Nachfolger#1623884


$keywords = ISSET($_GET['keywords']) && is_string($_GET['keywords']);


What exactly critical portions missing ?

Anyway, why these incorrect ?

1.

if(ISSET($_GET['keywords']) && is_string($_GET['keywords']);)
{
$keywords = $_GET['keywords'];
}
else
{
die("Type your keywords");
}


2.

if(!ISSET($_GET['keywords']) || ! is_string($_GET['keywords']);)
{
die("Type your keywords");
}
else
{
$keywords = $_GET['keywords'];
}
Copy linkTweet thisAlerts:
@NachfolgerOct 07.2020 — > @developer_web#1624026 What exactly critical portions missing ?

Read the following

> @Nachfolger#1623884 [...] look at the proper format of a ternary statement--Which is what you're trying to accomplish here.

Code block #1 is correct, I was not talking about that--Hence the reason I quoted the incorrect portion of your post, not the code.
Copy linkTweet thisAlerts:
@developer_webauthorOct 12.2020 — @Nachfolger#1624035

Spotted my typo from copy & paste from my other files:

if(!ISSET($_GET['keywords']) || ! is_string($_GET['keywords']);)

Semi colon typo again like here:

https://www.webdeveloper.com/d/391429-why-get-no-match-session/7
Copy linkTweet thisAlerts:
@developer_webauthorOct 12.2020 — @Nachfolger#1624035

From what I learnt from you here:

https://www.webdeveloper.com/d/391429-why-get-no-match-session/7

code block 2 is incorrect.


2.

if(!ISSET($_GET['keywords']) || ! is_string($_GET['keywords']))
{
die("Type your keywords");
}
else
{
$keywords = $_GET['keywords'];
}


So, sticking to:


1.

if(ISSET($_GET['keywords']) && is_string($_GET['keywords']);)
{
$keywords = $_GET['keywords'];
}
else
{
die("Type your keywords");
}


Ok. Buddy!
Copy linkTweet thisAlerts:
@developer_webauthorDec 03.2020 — @criterion9.

What you think of my previous post's conclusion ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 07.2021 — @NogDog,

I am not so bright. What was NachFolger trying to tell me how I should have coded ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 18.2021 — @Nachfolger

What did you mean ? A code sample from you will speak a thousand words, what you meant.
×

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 3.29,
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: @darkwebsites540,
tipped: article
amount: 10 SATS,

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

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...