Burdie87
Burdie87

Reputation: 123

Join 4 Tables and retrieve data

I have been trying to join 4 tables together so I can retrieve data from each and display in a table on my webpage.

I have tinkered with the JOIN command but I cannot get anything to display.

My table schema is:

fuelrecords 

FR_ID (Auto increment) 
VEH_LIST_REG 
FR_WE 

fuelrecords_die 

FRD_ID (AUTO INCREMENT) 
FR_DIE_L 
FR_DIE_C 
fuelrecords_ID (foreign ID from fuelrecords) 

fuelrecords_pet 

FRP_ID (AUTO INCREMENT) 
FR_PET_L 
FR_PET_C 
fuelrecords_ID (foreign ID from fuelrecords) 

fuelrecords_oil 

FRO_ID (AUTO INCREMENT) 
FR_OIL_L 
FR_OIL_C 
fuelrecords_ID (foreign ID from fuelrecords) 

And my code on the page is:

<?php
$qf=$_GET["q"];
$dtrf=trim($_GET["q"]);
list($d, $m, $y) = explode('-', $dtrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$q=strftime('%Y-%m-%d',$mk);

//$q=$_GET["q"];

$con = mysql_connect( 'IP', 'username', 'password' );
if( !$con ) {
die( 'Could not connect: ' . mysql_error() );
} else {
mysql_select_db( 'jbsrint', $con );
$sql="SELECT fuelrecords.VEH_LIST_REG, 
    fuelrecords.FR_WE, 
        fuelrecords_die.FR_DIE_L, 
            fuelrecords_die.FR_DIE_C, 
                fuelrecords_pet.FR_PET_L, 
                    fuelrecords_pet.FR_PET_C, 
                        fuelrecords_oil.FR_OIL_L, 
                            fuelrecords_oil.FR_OIL_C 
                                FROM fuelrecords 
                                    JOIN fuelrecords_die ON fuelrecords.FR_ID=fuelrecords_die.fuelrecords_ID 
                                        JOIN fuelrecords_pet ON fuelrecords.FR_ID=fuelrecords_pet.fuelrecords_ID 
                                            JOIN fuelrecords_oil ON fuelrecords.FR_ID=fuelrecords_oil.fuelrecords_ID 
                                                WHERE fuelrecords.FR_WE= '".$q."'";
$result = mysql_query($sql);


echo "<H3><font size=\"4px\" color=\"FFFFFF\">Fuel Records Weekly Input</font></H3>
<table border='1'>
<form  name=\"FR_REV\">
<tr>
<th>Week Ending</th>
<th>$qf</th>
<tr>
<th>Vehicle Reg</th>
<th colspan=\"2\">Diesel Data</th>
<th colspan=\"2\">Petrol Data</th>
<th colspan=\"2\">Oil Data</th>
</tr>
<tr>
<td></td>
<td>Litres Used</td>
<td>Total Cost</td>
<td>Litres Used</td>
<td>Total Cost</td>
<td>Litres Used</td>
<td>Total Cost</td>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>
<td><input type=\"text\" name=\"VEH_LIST_REG[]\" value=\"".$row["fuelrecords.VEH_LIST_REG"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_DIE_L[]\" value=\"".$row["FR_DIE_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_DIE_C[]\" value=\"".$row["FR_DIE_C"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_PET_L[]\" value=\"".$row["FR_PET_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_PET_C[]\" value=\"".$row["FR_PET_C"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_OIL_L[]\" value=\"".$row["FR_OIL_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_OIL_C[]\" value=\"".$row["FR_OIL_C"]."\" readonly=\"readonly\"></td>
</tr>";
}
}
echo"<tr>
<td></td><td></td>
</tr> 
</form>
</table";


mysql_close($con);

?>

Apologies if this is simple but my knowledge of joins is weak and all the examples I could find on the web seemed to relate to joining 2 tables.

Any help as usual is gratefully received.

Alan

Upvotes: 1

Views: 149

Answers (1)

Alex L
Alex L

Reputation: 6492

It is because the query joins records from all 4 tables, and if can't find any then just ignore whole line. Try LEFT JOIN instead of JOIN.

For example:

$sql="SELECT fuelrecords.VEH_LIST_REG, 
fuelrecords.FR_WE, 
    fuelrecords_die.FR_DIE_L, 
        fuelrecords_die.FR_DIE_C, 
            fuelrecords_pet.FR_PET_L, 
                fuelrecords_pet.FR_PET_C, 
                    fuelrecords_oil.FR_OIL_L, 
                        fuelrecords_oil.FR_OIL_C 
                            FROM fuelrecords 
                                JOIN fuelrecords_die ON fuelrecords.FR_ID=fuelrecords_die.fuelrecords_ID 
                                    JOIN fuelrecords_pet ON fuelrecords.FR_ID=fuelrecords_pet.fuelrecords_ID 
                                        JOIN fuelrecords_oil ON fuelrecords.FR_ID=fuelrecords_oil.fuelrecords_ID 
                                            WHERE fuelrecords.FR_WE= '".$q."'";

Here are differences between JOIN types.

Upvotes: 1

Related Questions