Null return from values

hi all,
i working on a sandbox for CMS and i am stuck with a problem. i had nothing being displayed on page so i had to var_dump the variable and it showed me its null, whereas it has values in database against the id. where can i be going wrong? here is my code

    
function get_pages_by_id($page_id){
        $query = "SELECT * FROM pages WHERE id = '$page_id'";
        $result = mysql_query($query);
        confirm($result);
        if($page_set = mysql_fetch_array($result)){
            return $page_set;
        }
    }

<?php
    if(isset($_GET['subj'])){
        $sel_sub = $_GET['subj'];
        $sel_page = "";
    }
    elseif(isset($_GET['page'])){
        $sel_sub = "";
        $sel_page = $_GET['page'];
    }else{
        $sel_sub = "";
        $sel_page = "";
    }
    $sub = get_subjects_by_id($sel_sub);
    $page = get_pages_by_id($sel_page);
    ?>

the second bit of code, get subjects in subjects are clicked. which is working fine. but the page function (first php code) passes the value in url of id, but don’t echo anything. so i took var dump and it says null

Do an echo of $query in the function and see what it says.

And what happens if this ‘if’ in the functions is not true?

if($page_set = mysql_fetch_array($result)){

What will the function return?

i did print_r as echo didn’t work, so here is the result

Array (     [0] => 2     [id] => 2     [1] => 1     [subject_id] => 1     [2] => Our Mission     [menu] => Our Mission     [3] => 2     [position] => 2     [4] => 1     [visible] => 1     [5] => Our mission statement is     [content] => Our mission statement is )

i wonder if till here, the query and result are coming all correct. wht happens that when i pass argument in function, it return null

Don’t make assumptions about what key data is being passed to the function without checking them too.


     var_dump($sel_sub);
     var_dump($sel_page);


Selected Subject string '' (length=0) 			Selected Page string '1' (length=1) 

so the values swap, when i click subject it give this


Selected Subject string '1' (length=1) 			Selected Page string '' (length=0) 

i went ahead and took a var dump of


$sub = get_subjects_by_id($sel_sub);
    $page = get_pages_by_id($sel_page);

and as expected the value of $sub returned value, while $pages returned with Boolean false.what am i doing wrong?

found the problem. but i don’t know why its doing it, this is what i echo

            <?php echo $sub['menu_name']; ?>
            <?php echo $page['menu'];?>

it doesn’t echo the page name there, but if i put it somewhere lets say


<?php
    $sub = get_subjects_by_id($sel_sub);
    $page = get_pages_by_id($sel_page);
    ?>
        <div><?php echo $page['menu'];?></div>

it work. what can be the reason of this stupid bug?

There where? You only posted two lines of code.

if i call the function on top of it the echo and not in start of the page.like so,

    <h2><?php echo $sub['menu_name']; ?>
                
                <?php 
                $page = get_pages_by_id($sel_page);
                echo $page['menu'];?>
            </h2>
            
            <div><?php 
            echo $page['content']; 
            //print_r($pg);
            ?></div>

And this doesn’t work?

hahah, it does. but that is one strange thing. that it doesn’t work when function is called in start of page n work here,anyhow it works in the end :stuck_out_tongue: