/    Sign up×
Community /Pin to ProfileBookmark

Why The CONDITION Fails ?

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 ?

““

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorAug 14.2020 —  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.
Copy linkTweet thisAlerts:
@NogDogAug 14.2020 — At a wild guess, this is failing, so you never make it to the if() you're focusing on here:
<i>
</i>if(mysqli_stmt_prepare($stmt_2,$query_2))
{
Copy linkTweet thisAlerts:
@developer_webauthorAug 17.2020 — @NogDog#1622119

Wrong!

Look at the code comments. I updated just for you to understand things better.

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


Read the code comments NogDog.

This is puzzling. Right ?
Copy linkTweet thisAlerts:
@NogDogAug 17.2020 — A while() is not an else. It does not automatically execute its code block. It only gets executed if the while expression evaluates a true, so probably the mysqli_fetch_array() is returning false. (I.e., no rows were returned by the query.)
Copy linkTweet thisAlerts:
@developer_webauthorAug 17.2020 — @NogDog#1622221

After $query_1, I echoed $row_count and nothing got echoed. After $query_2, I echoed $row_count and it got 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.

I fixed my pagination. But small issue in the url where form submission sends you.

More typos were in the urls on the pagination section. Fixed them and pagination section now working fine. I am adding my latest below ...


<?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++;
}
}
?>


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 ?
Copy linkTweet thisAlerts:
@developer_webauthorAug 24.2020 — No one knows the answer ?
Copy linkTweet thisAlerts:
@hemangjoshi37aAug 31.2020 — Has anyone found already a answer yet?


Hemang Joshi,

Web Designer,

**Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.**
Copy linkTweet thisAlerts:
@developer_webauthorAug 31.2020 — @Sempervivum

Do you mind closing this thread after answering my post:

https://www.webdeveloper.com/d/390993-why-the-condition-fails/6

Cheers.
×

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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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