aron pascua aron pascua - 1 year ago 69
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

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

here is my code:


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_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);


<td>FULL NAME</td>

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

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

Answer Source

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

$query = "SELECT * FROM `crew_info` WHERE `crew_rank` IN ".
        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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download