Rebekah Rebekah - 12 days ago 8
MySQL Question

display mysql data in a checkbox

I am trying to us php to pick up information from my mysql (phphmyadmin) database and display it with checkboxes, when I run my code I can display the radio button but no text seems to display from the database. Any ideas why this is?

Here is my code, I linked to my database in another file which I have linked in my code but that is not shown here.



<?php

$query1 = "SELECT town FROM place WHERE ID = 1";


$result1 = mysqli_query($con, $query1) or die("Invalid Query");

while($row1 = mysqli_fetch_row($result1)){

$town = $row1["town"];


echo "<input type=\"checkbox\" name=\"town\" value=\"$town\" />$town";

}
?>




Answer

It should work if you use the function mysqli_fetch_assoc instead of mysqli_fetch_row because then you have the array ready as you use it. With mysqli_fetch_row the result array is only indexed with ascending index - no names.

while($row1 = mysqli_fetch_assoc($result1)){ ...

instead of

while($row1 = mysqli_fetch_row($result1)){ ...

Note: You shouldn't use the mysqli-interface at all, use PDO.

Read: http://www.phptherightway.com