IF和ELSE都不会触发php脚本流!为什么?

如何解决IF和ELSE都不会触发php脚本流!为什么?

伙计,

在条件之前,我并没有杀死脚本流

die();

因此,两个触发之一应触发: 如果 否则

但是他们没有。

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 ?
        

第92行(IF)应该触发,或者第98行(类似于ELSE)触发。

请注意代码中的注释,以了解脚本无故终止流的位置。

<?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");
    }

什么是杀死脚本流?

上下文:

<?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,$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,$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!");
}

此外,在这2个中,哪个是正确的?

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

更新 我的代码现在看起来像这样。 在CAPITALS的代码注释中清楚地提到了我的问题。

当前问题:

问题1: 分页部分仅显示从PAGE 1到PAGE 1的链接。无法显示到PAGE 2的链接。配置为每页显示1行的代码。由于有2个匹配的行,因此自然会有2页显示所有结果。为什么分页部分无法显示从PAGE 1到PAGE 2的链接?

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,$query_1))
{
    echo __LINE__; echo "<br>";//LINE 57: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. 
    
    mysqli_stmt_bind_param($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_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,$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,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_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++;
    }
}
?>

再次更新: 问题:Url“&page =”中缺少。

看这个html:

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

如您所见,单击表单按钮后,您应该被发送到: pagination_test_2.php?keywords = $ keywords&limit = $ limit&page = 1“

但是你猜怎么着?您被发送到: ?keywords = $ keywords&limit = $ limit&search = search“

URL中“&page = 1”部分在哪里? 另外,如何不将“ search = search”添加到网址中?

<?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,$query_1))
{
    echo __LINE__; echo "<br>";
    
    mysqli_stmt_bind_param($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",$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,$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_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++;
    }
}
?>

错别字位于分页部分的网址上。现在,我修复了它们,因此分页部分工作正常。 有人可以友好地签出我的代码,并让我知道如果不是真的需要淘汰哪些行。

解决方法

我认为这两行都没有达到,因为该语句有效,但返回零行。在$query_2 = ...之前,您可以在$row_count上添加检查以查看是否有任何行吗?

要回答第二个问题,$total_pages = ceil($row_count/$limit);是正确的答案,因为mysqli_stmt_bind_result返回一个布尔值,因此$result_1是对还是错。

我个人喜欢使用面向对象的样式,而不喜欢您使用的过程样式,因为我发现它更易于阅读,但选择是您自己的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-