如果For循环中的条件

I have a query checking with $userid

$loginid = 2;
$sql = mysql_query( 'SELECT cc.name, c.fullname, c.userid FROM mdl_course c, mdl_coursecat cc WHERE c.course = cc.id');
echo '<table>';
while($row = mysql_fetch_array($sql))
{
  $userid = $row['userid'];
  echo '<tr><td>';
  if($userid == $loginid)  
  {
    echo '<a href="">go to course</a>';
  }
  else
  {
    echo 'you dont have permissions to go';
  }
  echo '</td></tr>';
}
echo '</table>';      

My problem is that it's not going inside the else statement. Could anyone suggest what the problem might be?

The first very big problem is that you use $sql as an array but mysql_query() return a resource. What you need is to use mysql_fetch_assoc(). Anyway because $ss will be an element of the array you have to use it according to its type (array and not object). Thus, you should have something like:

while ($ss = mysql_fetch_assoc($sql))
{
   $userid = $ss['userid'];

   // Do other stuff
}

The second one is that you have not escaped the single quote inside your sentence in the else.

Replace foreach($sql as $ss) with while($ss = mysql_fetch_object($sql)). Then the loop will actually work. You cannot iterate over a mysql resultset directly but need to use one of the mysql_fetch_* functions to retrieve a row.

To get rid of the syntax error, replace echo 'you don't have permissions to go'; with echo 'you don\'t have permissions to go'; or echo "you don't have permissions to go";.

Here: echo 'you don't have permissions to go'; Replace with echo 'you don\'t have permissions to go'; It sees the apostrophe of don't as end of string

i'd say the problem lies here:

while($row = $mysql_fetch_array($sql))

as mysql_fetch_array() is a function, not a variable.