drgbpq5930 2017-08-16 22:23
浏览 55
已采纳

将php变量从一个文件传递到另一个文件,而不是if语句的范围

How do I use a php variable which is transferred from one file (search.inc.php) into another (anotherpage.php) in if statement?

My connection with MySQL is correct but I can't use variable $bilkon (which is picked up from MySQL) in if statement in anotherpage.php.

When echo $bilkon in anotherpage.php, I see the "Yes" response, but when I use this $bilkon in if statement I don't get the correct response. I got the "Bad" response from the if statement although I got on echo answer "Yes".

search.inc.php

<?php
session_start();
require 'connect.php'; 

if(isset($_GET['search_text'])){
    $search_text = $_GET['search_text'];

    if(!empty($search_text)){

        $query="SELECT * FROM kon WHERE sifkon LIKE '%$search_text%'";
        $query_run = mysqli_query($con, $query);

        while($query_row = mysqli_fetch_assoc($query_run)){

            echo $sifkon='<a href="anotherpage.php?search_text='.$query_row['sifkon'].'">'.$query_row['sifkon'].'  '.'</a>';
            echo $nazkon = $query_row['nazkon'].'<br>';
            $bilkon = $query_row['bilkon'].'<br>';
            $strknj = $query_row['strknj'].'<br>';
            $devknj = $query_row['devknj'].'<br>';
            $orjkon = $query_row['orjkon'].'<br>';
            $tiporj = $query_row['tiporj'].'<br>';
            $_SESSION['test']=$bilkon;
        }
    }
}
?>

anotherpage.php

<?php
include 'search.inc.php';

echo  $_SESSION['test'];//Yes
if ($_SESSION['test'] == 'Yes'){
    echo 'Good';
}else {
    echo 'Bad';
}
?>
  • 写回答

1条回答 默认 最新

  • duanchao9559 2017-08-16 22:27
    关注

    $bilkon has a <br> at the end, you add it, but you dont check for that

    if ($_SESSION['test'] == 'Yes<br>'){
    

    better option to remove the <br> until you need it.

    for future debugging use

    var_dump($_SESSION['test']);
    

    instead of echo, it catches things like html, white space, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?