无法在php mysql中获取正确的数据

无法在php mysql中获取正确的数据,php,mysqli,Php,Mysqli,表:请求 id friend_id candidate_id == ============== ============== 1 20180928115958 20180925112428 2 20181008181142 20180925112428 3 20180928115958 20181008181142 4

表:请求

id          friend_id           candidate_id
==          ==============      ==============
1           20180928115958      20180925112428

2           20181008181142      20180925112428

3           20180928115958      20181008181142

4           20181010181207      20180928115958

5           20181008181142      20181010181207

6           20181010181207      20180925112428
质疑

预期产量

20180928115958

20181008181142

20180925112428
在这个代码中,我有一个表
request
,它有两个关键字段,即
朋友id
候选人id
。现在,我
$\u会话['candidate\u id']='20181010181207'
。那么,我怎样才能得到我上面提到的预期输出呢?请帮帮我

谢谢

试试这个

我希望它能起作用

if($row_ac['friend_id']==$_SESSION['candidate_id']){
    echo $row_ac['candidate_id']."<br/><br/>";
else{
    echo $row_ac['friend_id']."<br/><br/>";
}
if($row\u ac['friend\u id']=$\u SESSION['candidate\u id'])){
echo$row_ac['candidate_id']。“

”; 否则{ echo$row_ac['friend_id']。“

”; }
因为在您的情况下,
条件始终为false,否则部分将执行,所以请尝试使用
如果else
条件并检查

尝试此项

我希望它能起作用

if($row_ac['friend_id']==$_SESSION['candidate_id']){
    echo $row_ac['candidate_id']."<br/><br/>";
else{
    echo $row_ac['friend_id']."<br/><br/>";
}
if($row\u ac['friend\u id']=$\u SESSION['candidate\u id'])){
echo$row_ac['candidate_id']。“

”; 否则{ echo$row_ac['friend_id']。“

”; }
因为在您的情况下,
条件始终为false,否则部分将执行,因此请尝试使用
如果否则
条件并检查

我认为您需要:

while($row_ac = mysqli_fetch_array($sql_ac))
{
    if($row_ac['friend_id'] != $_SESSION['candidate_id']
        echo $row_ac['friend_id']."<br/><br/>";
    }
    if($row_ac['candidate_id'] != $_SESSION['candidate_id']
        echo $row_ac['candidate_id']."<br/><br/>";
    }
}
我想你想要:

while($row_ac = mysqli_fetch_array($sql_ac))
{
    if($row_ac['friend_id'] != $_SESSION['candidate_id']
        echo $row_ac['friend_id']."<br/><br/>";
    }
    if($row_ac['candidate_id'] != $_SESSION['candidate_id']
        echo $row_ac['candidate_id']."<br/><br/>";
    }
}
你的要求是:

select 
  friend_id,
  candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id
因此,返回
:candidate\u id
列中
friend\u id
candidate\u id
列中的所有行是正常的:

4           20181010181207      20180928115958

5           20181008181142      20181010181207

6           20181010181207      20180925112428
现在你想要的是:

20180928115958 // this is candidate_id of row 4

20181008181142 // this is friend_id of row 5

20180925112428 // this is candidate_id of row 6
如果您只希望列“XXX_id”的值不等于
:candidate_id
,请尝试以下操作:

select 
  IF(friend_id = :candidate_id, '', friend_id) as friend_id,
  IF(candidate_id = :candidate_id, '', candidate_id) as candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id
这样,您将为与您的
:候选者\u id
匹配的列返回空值

现在,只需在显示之前添加一个测试,以避免出现空行:

if (!empty($row_ac['friend_id'])
    echo $row_ac['friend_id']."<br/><br/>";
if (!empty($row_ac['candidate_id'])
    echo $row_ac['candidate_id']."<br/><br/>";
这样,您将只返回一个结果:不等于
:candidate_id

然后只需执行echo$row\u ac['result\u id']。“

”即可获得您想要的内容

您的请求是:

select 
  friend_id,
  candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id
因此,返回
:candidate\u id
列中
friend\u id
candidate\u id
列中的所有行是正常的:

4           20181010181207      20180928115958

5           20181008181142      20181010181207

6           20181010181207      20180925112428
现在你想要的是:

20180928115958 // this is candidate_id of row 4

20181008181142 // this is friend_id of row 5

20180925112428 // this is candidate_id of row 6
如果您只希望列“XXX_id”的值不等于
:candidate_id
,请尝试以下操作:

select 
  IF(friend_id = :candidate_id, '', friend_id) as friend_id,
  IF(candidate_id = :candidate_id, '', candidate_id) as candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id
这样,您将为与您的
:候选者\u id
匹配的列返回空值

现在,只需在显示之前添加一个测试,以避免出现空行:

if (!empty($row_ac['friend_id'])
    echo $row_ac['friend_id']."<br/><br/>";
if (!empty($row_ac['candidate_id'])
    echo $row_ac['candidate_id']."<br/><br/>";
这样,您将只返回一个结果:不等于
:candidate_id


然后只需执行echo$row\u ac['result\u id']。“

”为了得到你想要的

它仍然显示着
20181010181207 20180928115958 20181010181207 2018092511248
@Mickellegerit正在做你要求它做的事情。它返回3行匹配候选人或朋友。然后,对于每一行,你同时输出候选人ID和朋友ID。也许,如果你告诉我们你想做什么,我们可以提供帮助。你想在
候选id
匹配时输出
好友id
吗?当
好友id
匹配时输出
好友id
吗?有两个如果:
如果($row\u ac['friend\u id')!=$\u会话['candidate id'{echo$row\u ac['friend\u id'}
-如果此id
20181010181207
同时出现在
friend\u id
candidate\u id
中,则conadidate\u idI需要相同。它将不会显示它仍然显示
20181010181207 20180928115958 20181010181207 2018092511248
@Mickellegerit正在执行您要求它执行的操作。它返回3行,其中atch candidate或friend。然后,对于这些行中的每一行,您将同时输出候选者ID和朋友ID。如果您告诉我们您正在尝试做什么,我们可能会有所帮助。您想在
candidate\u ID
匹配时输出
朋友ID
吗?如果想在
朋友ID
匹配时输出
朋友ID
吗(($row_ac['friend_id']!=$会话['candidate_id']{echo$row_ac['friend_id']}-如果此id
20181010181207
同时出现在
friend\u id
candidate\u id
中,我想要的conadidate\u idI也是一样的。它不会确切地显示我需要这个。谢谢@JeffYou,欢迎您!在sql级别上可能有更好的方法来实现这一点。但很难说它是否适合您的需要。请参阅@Mickelleger这是sql版本的答案!正是我需要的。谢谢@JeffYou!在sql级别上可能有更好的方法来实现这一点。但是很难说它是否符合您的需要。请参阅@Mickeelleger的sql版本答案!