php - populate listbox with table names -
i want populate listbox table names database. here's code i've written doesn't seem work
<select id="arrays" name="arrays" style="width: 403px;" class="fieldcell"> <?php $dbname = 'mybase'; if (!mysql_connect('localhost', 'root', '')) { echo 'could not connect mysql'; exit; } $sql = "show tables $dbname"; $result = mysql_query($sql); if (!$result) { echo "db error, 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 section make work. using loop trying loop through variable $row never defined. need use mysql_fetch_row() grab data result set. looks had, commented out. nuke lop , uncomment while loop. although looks have syntax error in while loop (missing quotes). here should like:
while ($row = mysql_fetch_row($result)) { echo "<option value='{$row[0]}'>{$row[0]}</option>"; }
or
you can keep way now, above loop need add
$row = mysql_fetch_all($result);
Comments
Post a Comment