Using WHERE clause with multiple left joins in php

I did it successfully LEFT JOIN

with PHP , however I am having a hard time adding a suggestion WHERE

so that I can select a unique record from all existing details in all tables where a match is found.

This is my code:

  <?php
$sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*\n"
    . " FROM staff staff\n"
    . "LEFT JOIN staff_bio \n"
    . " ON staff.nuc_id = staff_bio.bio_id\n"
    . "LEFT JOIN staff_cert\n"
    . " ON staff.nuc_id = staff_cert.pro_id\n"
    . "LEFT JOIN staff_edu\n"
    . " ON staff.nuc_id = staff_edu.edu_id\n"
    . "LEFT JOIN staff_pos\n"
    . " ON staff.nuc_id = staff_pos.rank_id\n"
    . "LEFT JOIN staff_res\n"
    . " ON staff.nuc_id = staff_res.res_id\n"
    . "WHERE staff.nuc_id = $userRow['staff_no'] ";//this is where i'm having issues
?>

      

My end result should look something like this:

<?php echo $userRow['sch_name']; ?>
<?php echo $userRow['fac_name']; ?>
<?php echo $userRow['dep_name']; ?>

      

Everything comes from different tables. Any help would be greatly appreciated.

+3


source to share


1 answer


You can do the following:

. " WHERE staff.nuc_id = ".$userRow['staff_no'] ;//this is where i'm having issues

      



Why do you need to concatenate the query, it can be written as if you want to format:

<?php 

    $sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*
         FROM staff staff
        LEFT JOIN staff_bio 
         ON staff.nuc_id = staff_bio.bio_id
        LEFT JOIN staff_cert
         ON staff.nuc_id = staff_cert.pro_id
        LEFT JOIN staff_edu
         ON staff.nuc_id = staff_edu.edu_id
        LEFT JOIN staff_pos
         ON staff.nuc_id = staff_pos.rank_id
        LEFT JOIN staff_res
         ON staff.nuc_id = staff_res.res_id
        WHERE staff.nuc_id = ".$userRow['staff_no'];//this is where i'm having issues

     ?>

      

0


source







All Articles