0
$connect_status = "SELECT connected_to_id FROM tbl_connect WHERE user_id = 
{$_SESSION['user_id']}";

$exec_connstatus_query = mysql_query($connect_status, $db_connect);

while($check_status = mysql_fetch_array($exec_connstatus_query))
{   
  if ($check_status['connected_to_id'] == $profileid)
  {
    echo "{$check_status['connected_to_id']}";
  }
  else
  {
    echo "fail";
  }
}

This code has been troubling me for 3 hours. Here the issue is even if this condition:

if ($check_status['connected_to_id'] == $profileid)

returns false, I am not able to see "fail" when the else statement executes. Any ideas?

9
  • 1
    Are you sure the loop is being executed? Commented Jun 15, 2012 at 15:47
  • What kind of values can be in that connected_to_id field, and what's the value in $profileid? PHP will consider many comparison to be TRUE, even though any reasonable person wouldn't, e.g. the field in the database is NULL, and $profileid is 0 or an empty string. Commented Jun 15, 2012 at 15:48
  • Is this all your code? if you echo $check_status['connected_to_id'] and $profileid, what do they show? Should there be another '}' to close the while loop, or is it just missed in the question. Commented Jun 15, 2012 at 15:48
  • Is the first line with {} a valid php syntax? Can someone confirm? Commented Jun 15, 2012 at 15:49
  • 2
    @subirkumarsao: yes, it's valid. it's PHP syntax for embedding "complex" vars into double-quoted strings. e.g. $x ="$arr[a][b]" would normally fail because PHP's parser isn't gready and would only treat $arr[a] as an array, followed by a couple brackets and a b. using {} around the whole thing forces php to treat both bracket bits as part of the array dereference. Commented Jun 15, 2012 at 15:50

2 Answers 2

1

mysql_query() returns FALSE on error and the loop won't execute at all. This would occur if $_SESSION['user_id'] is not numeric. why not just

$connect_status = "SELECT connected_to_id FROM tbl_connect WHERE user_id ='$_SESSION[user_id]'";

Ditto mysql_fetch_array() returns FALSE if there are no (more) rows. If your session doesn't exist in tbl_connect then the loop won't execute. Don't you want something like:

$connect_status = "SELECT connected_to_id FROM tbl_connect 
                   WHERE user_id ='$_SESSION[user_id]' LIMIT 1";
$exec_connstatus_query = mysql_query($connect_status, $db_connect);
if( ($check_status = mysql_fetch_array($exec_connstatus_query)) && 
    ($check_status['connected_to_id'] == $profileid) ) {
    echo "{$check_status['connected_to_id']}";
} else {
    echo "fail";
}
Sign up to request clarification or add additional context in comments.

1 Comment

my Q status now is "edited edited 53 mins ago"answered 1 hour ago" Yr comment was timestamped an hr ago => you made a comment so I updated my answer accordingly.
0

I would suggest you to check $exec_connstatus_query first, because it may be that your query is not executed properly and thats why control did not go inside while loop. And your response is null. Print $exec_connstatus_query and check first.
Or in case if query return Zero records, in that case loop won't run.

1 Comment

Thanks! btw I am from India, wanna join for a project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.