-3

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';
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You asked the [same question](https://stackoverflow.com/q/45722728/4265352) one hour ago and it was closed as duplicate. – axiac Aug 16 '17 at 22:29
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – mickmackusa Aug 16 '17 at 22:30
  • @axiac I know and they went and deface the original post from it, without marking it as edits. I had to roll it back to its original state. – Funk Forty Niner Aug 16 '17 at 22:31
  • Regarding the title, there is no such thing as "scope of if statement". Have you read the answers on [this question](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and)? – axiac Aug 16 '17 at 22:35

1 Answers1

1

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