Reputation: 27
My table auto increments the value KundeID
and on the form that confirms the input of data I want to show the person inputting it what their ID is when they're done. I'm using this code currently and while I get the correct ID, it loops forever, but I only want it to tell the ID once. What can I do to fix this?
$sq="SELECT MAX(KundeID) AS max_ID FROM kunder";
while ($result=mysqli_query($kobling,$sq)) {
$row=mysqli_fetch_array($result);
echo "Ditt kundenummer er ".$row["max_ID"]."<br>";
}
Upvotes: 1
Views: 28
Reputation: 14746
As you mention table auto increments the value KundeID and you want maximum or latest Id, Right ?
So no need to use while loop you can directly get value using query your code should be like this:
$sq="select KundeID from kunder order by KundeID desc limit 1";
$result=mysqli_query($kobling,$sq);
$row=mysqli_fetch_array($result);
echo "Ditt kundenummer er ".$row["KundeID"]. "<br>";
This may help you.
Upvotes: 0
Reputation: 54831
Your solution is:
$sq="SELECT MAX(KundeID) AS max_ID FROM kunder";
$result = mysqli_query($kobling,$sq);
$row = mysqli_fetch_array($result);
echo "Ditt kundenummer er " . $row["max_ID"] . "<br>";
You don't need to use while
here.
Upvotes: 1