Tuesday, 28 August 2018

mysql query does not display the correct output

I have this mysqli query that will display all the data that the crew_status is PENDING FOR LINEUP but with my query right now, it displays all the data that is in database

Here is my query:
<?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);

?>

Can someone tell what is the problem?
    <?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>';
    }
    ?>


Wrap your or with (), try this:
SELECT *
FROM `crew_info`
WHERE (
    `crew_rank` = 'MASTER'
    OR `crew_rank` = 'CHCK'
    OR `crew_rank` = 'MESSMN'
    OR `crew_rank` = '2E'
    OR `crew_rank` = '2M'
    OR `crew_rank` = 'COOK'
    OR `crew_rank` = 'OLR'
    OR `crew_rank` = 'AB'
    OR `crew_rank` = '3E'
    OR `crew_rank` = '3M')
AND `crew_status` = 'PENDING FOR LINEUP'

Without (), if one of these ORs matches, then condition left will not be computed.
See this:
true || false && false = true
(true || false) && false = false

And I think you'd better use IN instead of OR like :
SELECT *
FROM `crew_info`
WHERE
    `crew_rank` IN ('MASTER','CHCK','MESSMN','2E','2M','COOK','OLR','AB','3E','3M')
AND `crew_status` = 'PENDING FOR LINEUP'

Edited:
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 = ?

0 comments:

Post a Comment