跳过具有特定ID的行

时间:2015-07-10 06:54:54

标签: php mysql

当记录与变量值匹配时,我想使用php从mysql中跳过一条记录。

$row['uni_id']在我的数据库表的一行中是id,$_SESSION['uid']在会话中存储一个id。当id匹配时,应该跳过该记录。但它不起作用。你看到了这个问题吗?

while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
    if ($row['uni_id'] == $_SESSION['uid']){
        continue;
    }
    $_SESSION['new_img']=$row['pic_add'];enter code here
    $image= $row['pic_add']; 
    echo "<tr><td>"; 
    echo "{$row['fname']} {$row['lname']}";
    echo "</td><td>";   
    echo $row['street'];
    echo "</td><td>";  
    echo $row['city'];  
    echo "</td><td>"; 
}
mysql_free_result($res); 

6 个答案:

答案 0 :(得分:0)

您可以尝试以下代码吗?

<?php
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
    if ($row['uni_id'] != $_SESSION['uid']){
        $_SESSION['new_img'] = $row['pic_add'];
        $image= $row['pic_add']; 
        echo "<tr><td>"; 
        echo "{$row['fname']} {$row['lname']}";
        echo "</td><td>";   
        echo $row['street'];
        echo "</td><td>";  
        echo $row['city'];  
        echo "</td><td>"; 
    }       
}
 mysql_free_result($res); 
?>

答案 1 :(得分:0)

我认为你是以错误的方式使用条件。

以下代码可能会对您有所帮助。

while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] == $_SESSION['uid']){
    // do nothing
}
else{ 
    $_SESSION['new_img']=$row['pic_add'];
    $image= $row['pic_add']; 
    echo "<tr><td>"; 
    echo "{$row['fname']} {$row['lname']}";
    echo "</td><td>";   
    echo $row['street'];
    echo "</td><td>";  
    echo $row['city'];  
    echo "</td><td>"; 
}

}

mysql_free_result($res);

答案 2 :(得分:0)

在开始用PHP处理结果之前,您应该调整MySQL查询。

解决方案取决于您的查询。如果您的查询是这样的:

select * from table

您应该将其更新为:

select * from table WHERE uid <> "$_SESSION['uid']"

如果无法控制MySQL查询,则可以更改PHP代码:

 while($row = mysql_fetch_array($res,MYSQL_ASSOC )){

        if ($row['uni_id'] != $_SESSION['uid']){

        $_SESSION['new_img']=$row['pic_add'];enter code here
        $image= $row['pic_add']; 
         echo "<tr><td>"; 
      echo "{$row['fname']} {$row['lname']}";
      echo "</td><td>";   
      echo $row['street'];
      echo "</td><td>";  
     echo $row['city'];  
      echo "</td><td>"; 
       }
    }
    mysql_free_result($res);

答案 3 :(得分:0)

试试这个

while($row = mysql_fetch_array($res,MYSQL_ASSOC )){

    if ($row['uni_id'] != $_SESSION['uid']){

    $_SESSION['new_img']=$row['pic_add'];
    $image= $row['pic_add']; 
    echo "<tr><td>"; 
    echo "{$row['fname']} {$row['lname']}";
    echo "</td><td>";   
    echo $row['street'];
    echo "</td><td>";  
    echo $row['city'];  
    echo "</td><td>"; 
    }
}
    mysql_free_result($res); 

答案 4 :(得分:0)

试试这段代码片段

    while($row = mysql_fetch_array($res))
    {
        $t='';
        if($row['uni_id'] != $_SESSION['uid'])
        {
            $_SESSION['new_img']=$row['pic_add'];//enter code here
            $image= $row['pic_add'];
            $t.="<tr>";
            $t.="<td>{$row['fname']} {$row['lname']}</td>";
            $t.="<td>".$row['street']."</td>";
            $t.="<td>".$row['city']."</td>";
            $t.="</tr>";
        }
        echo $t;
    }
    mysql_free_result($res);

答案 5 :(得分:0)

while($row = mysql_fetch_array($res,MYSQL_ASSOC )){

  if ($row['uni_id'] !== $_SESSION['uid']){

    $_SESSION['new_img']=$row['pic_add'];
    $image= $row['pic_add']; 
    echo "<tr><td>"; 
    echo "{$row['fname']} {$row['lname']}";
    echo "</td><td>";   
    echo $row['street'];
    echo "</td><td>";  
    echo $row['city'];  
    echo "</td><td>"; 

  }

}
mysql_free_result($res); 

注意:不要使用mysql。它被弃用了。使用PDO。