条形码不能读取mysql中的行

Good evening to all, I would like to print a bar code for each type of pasta. I adapted this code to mine, but it does not read the data that is within the code field. (Which is created by the php rand () code) I would choose the id through the bar code to be printed.

<html>
<head>
<?php
$con=mysqli_connect("localhost","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id'];
$query= "select * from pasta where id='$id'";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)){
$rand=$row['codice'];
}
?>
<br>
<div style="width:25px: height:500px; border:2px, solid #000; align="center"">
<div align="center">
<img src="http://bcgen.com/demo/linear-dbgs.aspx?=D<?php echo $rand  ?>">
<br>
<form action ="stampa.php" method="post">
<input  type="button" value="stampa"
onClick="window.print()"/>
</form>
</body>
</html>

It seems to me you have a loop that reads all rows matching the select, and in a loop overwrite all values of 'codice' but the last, and then create some html that will (eventually, in the browser) generate your barcode.

Solarflare is quite correct: check that your input is what you think it is, and that the query result has the fields you expect, too. It is considered good practice to always mention field names explicitly -- that is, don't use * -- so that mismatched names are easier to spot.

If you are only expecting one value from the DB then you don't need a while loop, and perhaps a LIMIT 1 in the SQL would be good too. If not, then you need to nest the html within the php loop, so you get more than one img tag output.

... and definitely fix the html. It would help if you indented the code as this immediately shows where things don't match up.

[Talking of which, better practice would be to generate the barcode server-side, not make the browser/client do it from a 3rd party site.]