如何在PHP中使用OR条件正确的MySqli SELECT?

时间:2016-07-27 06:49:59

标签: php mysql mysqli

我试图显示所有数据作为我的query,但不知何故,我不知道如何使用mysqli编写的

这是我的代码:

<?php

include '../session.php';
require_once 'config.php';
include 'header.php';

    $master = 'MASTER';
    $chck = 'CHCK';
    $second_engineer = '2E';
    $second_mate = '2M';
    $third_engineer = '3E';
    $third_mate = '3M';
    $ce = 'CE';
    $bsn = 'BSN';
    $ab = 'AB';
    $olr = 'OLR';
    $dcdt = 'DCDT';
    $egdt = 'EGDT';
    $cook = 'COOK';
    $messman = 'MESSMN';
    $crew_status = 'PENDING FOR LINEUP';
    $query = "SELECT * FROM `crew_info` WHERE `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? OR `crew_rank` = ? AND `crew_status` = ?";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, 'sssssssssssssss', $crew_status, $master, $chck, $second_engineer, $second_mate, $third_engineer, $third_mate, $ce, $bsn, $ab, $olr, $dcdt, $egdt, $cook, $messman);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $first_name, $middle_name, $last_name, $age, $month, $day, $year, $birth_place, $gender, $martial_status, $religion, $nationality, $email_address, $address_1, $address_2, $course, $school_graduated, $remarks, $note, $date_added, $crew_status, $crew_rank, $image_name, $updated_photo, $passport_registration, $passport_expiration);



?>

<table>
    <tr>
        <td>FULL NAME</td>
        <td>RANK</td>
        <td>STATUS</td>
    </tr>
    <tr>
        <?php

        while(mysqli_stmt_fetch($stmt)) {
            echo "<tr>";
            echo "<td>".sprintf("%s%s%s", $first_name, $middle_name, $last_name)."</td>";
            echo "<td>".sprintf("%s", $crew_rank)."</td>";
            echo "<td>".sprintf("%s", $crew_status)."</td>";
            echo '</tr>';
        }
        ?>
    </tr>
</table>

这段代码没有给我输出。只有普通页面

1 个答案:

答案 0 :(得分:1)

在编程中,KISS校长会说。您的代码可以大大简化

$query = "SELECT * FROM `crew_info` WHERE `crew_rank` IN ".
         "('MASTER','CHCK','2M','3E','3M','CE','BSN','AB','OLR','DCDT','EGDT','COOK','MESSMN')
        AND `crew_status` = 'PENDING FOR LINEUP'";

你甚至不需要这里准备好的声明,因为你只使用常量。

如果您继续获得空白页面,您是否认为数据库中可能没有任何匹配的行?请将以上内容粘贴到mysql控制台并显示结果。