Jump to content

Mysterious Resetting Variable


Shadow Jolteon

Recommended Posts

Hello,

 

I have been working on a page for a couple days now for my website, and I seem to have hit a bump in the road, and I can't figure out what the problem could possibly be. The code below is what I'm having trouble with. The variable $watchPost gets reset (returning a 0) after the first if statement, and all variables that used the $watchPost variable adjust accordingly ( watchPosition returns 0, watchPlace returns 1 ). Before the if statement, everything works fine, however. The other two variables in the bind_result seem to work fine.

 

        if( $loginId && $threadSub = $mysqli -> prepare( $threadSubSql ) ) {
            $threadSub -> bind_param( "ss", $loginId, $arrKey );
            $threadSub -> execute();
            $threadSub -> bind_result( $watchType, $watchTime, $watchPost );
            $threadSub -> fetch();

            $watchPosition  = $watchPost;
            $watchPlace     = $watchPosition + 1;

            if( $watchTime < $thread[$arrKey]["puretime"] ) {
                // watchPost variable reset here?
                $thread[$arrKey]["newpost"] = $watchPosition;
                
                if( $watchPlace >= $loginThreadPage + 1 )
                    $watchPage = "page/" . floor( $watchPost / $loginThreadPage );
                
                $thread[$arrKey]["postlink"]    = $watchPage . "#post" . $watchPlace;
                $thread[$arrKey]["newmark"]     = ' class="thread_unread"';
            }
            
            $thread[$arrKey]["sub"] = $watchType;
            unset( $watchType, $watchTime, $watchPost, $watchPage, $watchPosition, $watchPlace );
            
            $threadSub -> close();
        }

 

This issue is driving me crazy now, so I would greatly appreciate any replies.

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/265006-mysterious-resetting-variable/
Share on other sites

How did you confirm that $watchPost is changing value after the if statement? Did you try something like this

echo "watchPost before if() statement: {$watchPost}<br>\n";
if( $watchTime < $thread[$arrKey]["puretime"] ) {
echo "watchPost after if() statement: {$watchPost}<br>\n";

 

I don't see anywhere in that code that $watchPost is actually used in output, so I'm curious how you verified that hypothesis. I don't see anything in the code that would cause that so there is probably some other explanation. It would be a good idea to echo all/most of your variables to the page at key points in the logic to verify they contain what you think they contain.

I echoed the variable immediately before, inside, and after the if statement, the results for the first result of the $watchPost variable being: 60, 0, 0

 

I echoed everything in this area of my coding (and some from before) at several points throughout my file, all with the same results: anything before the if is fine. I have actually rewritten this entire section, as well, and the same thing keeps happening.

 

Thanks! =)

Forgive me for being skeptical, but I've had similar situations before and there is always a logic explanation. Can you try the two lines I showed above EXACTLY as I put them - one immediately before the if() statement and one immediately after? In fact, I would suggest echoing the values of the variables used in the if() condition as well. Give this a try and post the results:

 

echo "watchPost before if() statement: {$watchPost}<br>\n";
echo "watchTime before if() statement: {$watchTime}<br>\n";
echo "arrKey before if() statement: {$arrKey}<br>\n";
echo "thread[arrKey]['puretime'] before if() statement: {$thread[$arrKey]['puretime']}<br>\n";
if( $watchTime < $thread[$arrKey]["puretime"] ) {
echo "watchPost after if() statement: {$watchPost}<br>\n";
echo "watchTime after if() statement: {$watchTime}<br>\n";
echo "arrKey after if() statement: {$arrKey}<br>\n";
echo "thread[arrKey]['puretime'] after if() statement: {$thread[$arrKey]['puretime']}<br>\n";

Hey, thanks a ton for that, I found the issue after replacing the if statement with that. It turns out that I was getting confused in my own echoing of the variables because I forgot to add a line break after a couple of them and it kinda messed it up. So, it turns out there was not any problem at all, other than with myself. =p

 

Thank you very much for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.