I am having some trouble with selecting data from my database using data from a previous select. What I want to do is. First I let mysql select all vechiles from my database from the table vechiles. I echo them and inside the while loop I try to select the picture for the vechile from a diffrent table by select the picture with the vechile_id:
Now my first question is. Is this the right way to do it? I assume there must be a more neat way but I cant find how.
<?php
$sql = "SELECT id, brand FROM vechiles";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sql1 = "SELECT * FROM fotos WHERE vechiles_id =". $row['id'];
$result1 = $conn->query($sql1);
echo '<li><p class="title">'. $row['brand'] .'</p>
<p class="type">type: 2387</p>
<p class="ez">ez: 1987</p>';
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo '<img src="admin/'. $row1['url'] .'" atl="tractor 1"/>';
}}else{
echo "there are no pictures";
}
echo '<div class="info">
<p class="prijs">Prijs: 1800,- EX BTW</p>
<p class="msg"> Prijzen onder voorgehoud</p>
</div>
<a class="button" href="#">Meer info</a></li>';
}
} else {
echo "0 results";
}
$conn->close();
?>
Noe the above example works fine but I want to only select the first picture instead of all uploaded pictures. So to this code:
$sql1 = "SELECT * FROM fotos WHERE voertuig_id =". $row['id'];
I added
$sql1 = "SELECT TOP 1 * FROM fotos WHERE voertuig_id =". $row['id'];
And I also tried
$sql1 = "SELECT * FROM fotos WHERE voertuig_id =". $row['id'] . "LIMIT 1";
But when I do that it suddenly says there are no pictures. What am I doing wrong!
appreciate all the help!