2

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>';
    }
    ?>
1
  • cover all OR conditions for crew_rank in () and check Commented Jul 27, 2016 at 5:15

4 Answers 4

3

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 = ?
Sign up to request clarification or add additional context in comments.

7 Comments

can you explain me this one? why my query does not display the right output and yours displays the right output?
how to do this in mysqli prepared ?
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 = ?` how can I do that in my code?
OK, here you just give us sql query, but no php code. I can't do more than this. @aronpascua
No, code like so does not make sense. You should post more about this query.
|
1

Some changes in your query:

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')

Hope it will help you.

Comments

0

The ANDs and ORs are ambiguous. It's unclear if you want something like:

(A AND B) OR C

or

A AND (B OR C)

Add some parenthesis to clear things up:

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'

Comments

0

SELECT * FROM crew_info WHERE crew_rank IS NOT NULL AND crew_status = 'PENDING FOR LINEUP' I hope this would be worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.