<?php
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'xxxxxxxxxxxxxxxx');
DEFINE ('DB_NAME', 'bigbobsveterinarysurgery');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect, check the details ' . mysqli_connect_error());
mysqli_set_charset($dbc, 'utf8');
$q = "SELECT dog_name, owner_name, vet_date, Breed, cost, address, contact_number FROM owner, dogs ORDER BY dog_name ASC";
$r = @mysqli_query ($dbc, $q);
echo '<table>
<tr>
<th scope="col">Dog</th>
<th scope="col">Owner</th>
<th scope="col">Entry Date</th>
<th scope="col">Breed</th>
<th scope="col">Cost</th>
<th scope="col">Address</th>
<th scope="col">Contact Number</th>
</tr>'
;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr><td>' . $row['Dog_name'] . '</td><td>' . $row['owner_name'] . '</td><td>' . $row['vet_Date'] .
'</td></tr>' . $row['Breed'] . '</td></tr> ' . $row['cost'] . '</td></tr> '. $row['address'] . '</td></tr> ' . $row['contact_number'] . '</td></tr> ';
}
echo '</table>';
?>
It is displaying the breed, cost and contact number of all the data in one big lump. the format of the table i want is found underneath, but each dog is listed to every single other owner and all the other columns are blank. the release date isnt displaying, but i think thats a problem from when i imported the data from an excel sheet and the date was in the wrong format
EDIT
I have updated it to:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr><td>' . $row['Dog_name'] . '</td><td>' . $row['owner_name'] . '</td><td>' . $row['vet_Date'] .
'</td><td>' . $row['Breed'] . '</td><td> ' . $row['cost'] . '</td><td> '. $row['address'] . '</td><td> ' . $row['contact_number'] . '</td></tr> ';
}
This has stopped the whole mess at the beginnign and all the data is now formatted in a table. however, each dog is still matching with each single owner. is this a problem in the code in this post or the database?
I think you need to join the two tables
$q = "SELECT dog_name, owner_name, vet_date, Breed, cost, address, contact_number
FROM owner o
LEFT JOIN dogs d ON o.id = d.owner_id
ORDER BY d.dog_name ASC";
// make sure you join on valid columns.
// also, if there is a column that appears in both tables,
// to avoid ambiguity, prefix it with the corresponding table alias
$r = @mysqli_query ($dbc, $q);
echo "<table>
<tr>
// skipping this so it doesn't eat up too much space
</tr>";
// also, every `$row[something]` should be placed inside a `<td></td>`, like so:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr>
<td>' . $row['Dog_name'] . '</td>
<td>' . $row['owner_name'] . '</td>
<td>' . $row['vet_Date'] . '</td>
<td>' . $row['Breed'] . '</td>
<td>' . $row['cost'] . '</td>
<td>' . $row['address'] . '</td>
<td>' . $row['contact_number'] . '</td>
</tr> ';
}
Selecting from two tables causes a cartesian product between them.
Using a join
, you only get the corresponding lines (according to the ON
clause).
In your particular case, using join
will maintain the dog-owner relationship, whereas using FROM owner, dogs
, you would get all dogs for each owner, which may not be what you're after.