Reputation: 3
Alright so I've been trying to replace a number from the database for the actual name, unfortunately it only returns nothing at all.
I'm not sure what I'm doing wrong here, Tried it multiple ways, as explained on other related questions, yet it doesn't seem to work. The first while seems to work, but the second doesn't do its job..(although in this setup it gives nothing at all)
here is my code:
<?php
include_once 'db_connect.php';
$query = "SELECT bestel_nummer, bestel_datum, bestel_tijd, kkt_id, mwr_id
FROM bestellingen";
$response = mysqli_query($mysqli, $query);
if ($response){
echo '<table align="left" cellspacing="5" cellpadding="8">
<td align="left"><b>bestel nummer<b></td>
<td align="left"><b>bestel datum<b></td>
<td align="left"><b>bestel tijd<b></td>
<td align="left"><b>klanten nummer<b></td>
<td align="left"><b>Medewerker<b></td></tr>';
while($row = mysqli_fetch_array($response)){
$mwr_id = $row['mwr_id'];
$query2 = "SELECT 'voornaam' FROM 'medewerkers' WHERE 'mwr_id' = $mwr_id LIMIT 1";
$response2 = mysqli_query($query2, $mysqli);
while($row2 = mysqli_fetch_array($response2)){
echo '<tr><td align="left">'.
$row['bestel_nummer'].'</td><td align="left">'.
$row['bestel_datum'].'</td><td align="left">'.
$row['bestel_tijd'].'</td><td align="left">'.
$row['kkt_id'].'</td><td align="left">'.
$row2['voornaam'].'</td><td align="left">';
echo'</tr>';
}
}
echo'</table>';
}
else{
echo 'couldnt issue database query';
echo mysqli_error($mysqli);
}
mysqli_close($mysqli);
?>
Thanks in advance.
Upvotes: 0
Views: 54
Reputation: 13211
Maybe it's not the while loop, but the mysqli_query Seems like the arguments in the second query have a wrong order
first query
$response = mysqli_query($mysqli, $query);
second query
$response2 = mysqli_query($query2, $mysqli);
This means no result, no while loop, check the response2 variable, the while loop should work, but actually the code itself looks unperformant.
If you wanna have the most performance you should do it with one query, with something like Group and Join. I don't know your structure but it should be possible.
If you try to query the db, in a loop it's a alway a very bad idea, image 100, 1000 users will trigger this code.. ohoohooo your server will probably go down men.
Upvotes: 2