首先,第一次发布问题对我来说很容易:)
我正在尝试使用PHP / MySQL生成`Form DropDownList(Select / options)。我需要使用我的数据库中仅包含“.com”
的表名自动填充的选项这是我目前的代码:
<?php
$con = mysql_connect("localhost","username","password");
$db = mysql_select_db("dbname",$con);
$result = mysql_query("SHOW TABLES LIKE '%.com%'",$con) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {
$table = $tableName[0];
}
;
$option .= '<option value = "website-manager.php?table='.$table.'">'.$table.'</option>';
?>
<form name="form" id="form">
<select name="jumpMenu" id="jumpMenu">
<option value="#">Select Website</option>
<?php echo $option ; ?>
</select>
<input type="button" name="go_button" id= "go_button" value="Go" onclick="MM_jumpMenuGo('jumpMenu','parent',0)" />
</form>
当我使用此代码时,只填充一个表名,而不是我数据库中当前的15个。
如何更正上面的代码以填充所有表名?
答案 0 :(得分:2)
问题是你在每次迭代通过数据库结果集时都会覆盖$table
。
您需要将数据放入如下数组:
$tables = array();
while ($tableName...) {
$tables[] = $tableName[0];
}
然后在这样的循环中输出您的选项:
<option value="#">Select Website</option>
<?php
foreach($tables as $table) {
echo '<option value="website-manager.php?table=' . urlencode($table) .'">' . $table . </option>';
}
?>
答案 1 :(得分:0)
正如建议移动}
而在下面结束时 - 尝试用此替换部分:
<?php
$filter = '%wp_%';
$con = mysql_connect("localhost","root","pwd");
$db = mysql_select_db("enterDbName",$con);
$result = mysql_query("SHOW TABLES LIKE '".$filter."'",$con) or die('cannot show tables');
$option = '';
while($tableName = mysql_fetch_row($result)) {
$table = $tableName[0];
$option .= '<option value = "website-manager.php?table='.$table.'">'.$table.'</option>';
}
?><form name="form" id="form">
<select name="jumpMenu" id="jumpMenu">
<option value="#">Select Website</option>
<?php echo $option ; ?>
</select>
<input type="button" name="go_button" id= "go_button" value="Go" onclick="MM_jumpMenuGo('jumpMenu','parent',0)" />
</form>
警告:自PHP 5.5.0起,此扩展程序(mysql
)已弃用,将来将被删除。相反,应该使用MySQLi或PDO_MySQL扩展。