aron pascua aron pascua - 4 months ago 5
SQL Question

How to use MySqli SELECT with OR condition correct in PHP?

im trying to display all the data that is the as my

query
but somehow, i don't know how to do that using mysqli prepared

here is my code:

<?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>


this code does not give me an output. only plain page

Answer

In programming, the KISS principal appllies. Your code can be simplified hugely

$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'";

You don't even need a prepared statement here, because you are only using constants.

If you continue to get an empty page, did you consider that possibility that you may not have any matching rows in your database? Please copy paste the above into the mysql console and show the result.