IF语句在while循环中不起作用-不确定条件是否正确?

时间:2019-07-23 08:13:46

标签: php sql loops while-loop

我正在尝试从lend_status为1或2的数据库中回显条目,并编写了while循环以获取条目,但是我不确定IF语句是否正确或这是否是最佳方法去解决这个问题。我对PHP和SQL还是很陌生,所以我们将不胜感激!

我尝试使用回声查看中断的地方,这似乎是IF语句。我知道与数据库的连接有效,因为我可以从中回显信息并进行了测试。

<?php

// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
$STH = $DBH->prepare("SELECT * FROM laptop_system");
// DOESN'T HAVE TO BE AN ARRAY BUT GOOD PRACTICE TO PUT THIS IN (BELOW)
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS


while ($row = $STH->fetch()) {
    $lend_status = $row->lend_status;
    $lend_id = $row->lend_id;
    $requestee = $row->user_id;
    $first_name = $row->first_name;
    $last_name = $row->last_name;

    $active_status = array(1,2);

if($lend_status == $active_status) {

      echo 'hello';
?>

<div>
<?php echo $requestee_name . '\n'; ?>

<?php echo 'hi'; ?>

</div>
<?php }} ?>

1 个答案:

答案 0 :(得分:0)

问题是您正在将字符串/整数(数据库行的字段之一)与数组进行比较:

$lend_status = $row->lend_status;
...
$active_status = array(1,2);

if($lend_status == $active_status) {

如果要检查状态是否为数组中的值之一,则可以使用in_array()

if (in_array($lend_status, $active_status)) {