i want to populate a listbox with the table names from a database. here’s the code i’ve written for it but it doesn’t seem to work
<select id="arrays" name="arrays" style="width: 403px;" class="Fieldcell">
<?php
$dbname = 'myBase';
if (!mysql_connect('localhost', 'root', '')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$num_tables = mysql_num_rows($result);
for($i=0;$i<$num_tables;$i++)
{
echo "<option value=\"$row[i]\">$row[i]</option>";
}
/*while ($row = mysql_fetch_row($result)) {
echo <option value=\"$row[0]\">$row[0]</option>";
}*/
mysql_free_result($result);
?>
</select>
The commented out section is the section that will make it work. You are using a for loop where you are trying to loop through a variable $row that is never defined. You need to use mysql_fetch_row() to actually grab the data from the result set. Which it looks like you had, but then commented out. Nuke the for lop and uncomment the while loop. Although looks like you have a syntax error in your while loop (missing quotes). Here is what it should look like:
OR
You can keep it the way it is now, but above your for loop you need to add