我是PHP的新手,我正在制作此页面,如果记录中有任何值,我想显示数据库中的记录。我正在使用If condition
,但无法正常工作。这是我的代码:
$active_proof=FALSE;
while ($row_proof = mysqli_fetch_assoc($res_proof)){
if ($row_proof['proof_cat'] == "Proof of Address" && $row_proof['proof_cat'] == "Proof of Identity" ){
echo '<tr>
<td>'.$row_proof['proof_cat'].'</td>
<td>'.$row_proof['proof_doc'].'</td>
</tr>';
$active_proof = TRUE;
}else{
$active_proof = FALSE;
}
}
帮帮我!
答案 0 :(得分:1)
确定要检查proof_cat
的两个值吗?还是proof_cat
和proof_doc
?我认为应该...
$active_proof=FALSE;
while ($row_proof = mysqli_fetch_assoc($res_proof)){
if (($row_proof['proof_cat'] == "Proof of Address") && ($row_proof['proof_doc'] == "Proof of Identity" )){
echo '<tr>
<td>'.$row_proof['proof_cat'].'</td>
<td>'.$row_proof['proof_doc'].'</td>
</tr>';
$active_proof = TRUE;
}else{
$active_proof = FALSE;
}
}
答案 1 :(得分:1)
这段代码永远不会成真
if ($row_proof['proof_cat'] == "Proof of Address" && $row_proof['proof_cat'] == "Proof of Identity" ){
因为单列“ proof_cat”不能带有2个值。如果您是说OR,那么它将变为
if ($row_proof['proof_cat'] == "Proof of Address" || $row_proof['proof_cat'] == "Proof of Identity" ){
答案 2 :(得分:0)
字符串不能等于2个不同的值,我假设您要使用逻辑或(||
)而不是AND(&&
)。
使用in_array的一种干净方法,尤其是使用两个以上的值时,尤其如此:
if (in_array($row_proof['proof_cat'], ["Proof of Address", "Proof of Identity"])) {
答案 3 :(得分:0)
如果我不会误解您的代码,那么您需要此$row_proof['proof_cat']
和$row_proof['proof_doc']
而不是两个$row_proof['proof_cat']
来检查“ 地址”和“ 身份证明”,因为这是不可能的。所以我认为您做了一些 typo ,这就是为什么它不适合您的原因。
但是,如果您只想检查$row_proof['proof_cat']
的值是否相等,请使用in_array()代替
如果要检查是否等于
if (in_array($row_proof['proof_cat'], ["Proof of Address", "Proof of Identity"])) {
//your code will goes here
}
如果是错字:为什么我将其解释为错字?
您的代码块告诉我将其视为错字。
'。$ row_proof ['proof_cat']。'
'。$ row_proof ['proof_doc']。'
$active_proof=FALSE;
while ($row_proof = mysqli_fetch_assoc($res_proof)){
if ($row_proof['proof_cat'] == "Proof of Address" && $row_proof['proof_doc'] == "Proof of Identity" ){
echo '<tr>
<td>'.$row_proof['proof_cat'].'</td>
<td>'.$row_proof['proof_doc'].'</td>
</tr>';
$active_proof = TRUE;
}else{
$active_proof = FALSE;
}
}
我