Php 如何从数组中的特定Mysql表中获取列名
我正在尝试从mysql数据库中检索列名。我可以得到这样的列名:Php 如何从数组中的特定Mysql表中获取列名,php,html,mysql,select,Php,Html,Mysql,Select,我正在尝试从mysql数据库中检索列名。我可以得到这样的列名: Array ( [id] => id [BSPKey] => BSPKey [PrintPost] => PrintPost [Barcode] => Barcode [FirstName] => FirstName [LastName] => LastName [Add1] => Add1 [Add2] => Add2 [Suburb] => Suburb [State] =&
Array ( [id] => id [BSPKey] => BSPKey [PrintPost] => PrintPost [Barcode] => Barcode [FirstName] => FirstName [LastName] => LastName [Add1] => Add1 [Add2] => Add2 [Suburb] => Suburb [State] => State [Zip] => Zip [Team] => Team [Dteam] => Dteam [Fteam] => Fteam [DPID] => DPID [Error] => Error [Address1] => Address1 [Address2] => Address2 [correct3] => correct3 [correct4] => correct4 [correct5] => correct5 [fcorrect] => fcorrect [bspprint] => bspprint )
为了得到这个,我使用这个脚本
$qColumnNames = mysqli_query($conn,"SHOW COLUMNS FROM newdata");
$numColumns = mysqli_num_rows($qColumnNames);
$x = 0;
while ($x < $numColumns)
{
$colname = mysqli_fetch_row($qColumnNames);
$col[$colname[0]] = $colname[0];
$x++;
}
print_r($col);
$qColumnNames=mysqli_查询($conn,“显示来自新数据的列”);
$numColumns=mysqli\u num\u行($qColumnNames);
$x=0;
而($x<$numColumns)
{
$colname=mysqli\u fetch\u行($qColumnNames);
$col[$colname[0]]=$colname[0];
$x++;
}
印刷费($col);
但我只需要列名,并希望像这样填充html
元素
<select name="column_name[]" multiple="multiple">
<option selected="selected" value="id">id</option>
<option selected="selected" value="BSPKey">BSPKey</option>
<option selected="selected" value="PrintPost">PrintPost</option>
<option selected="selected" value="Barcode">Barcode</option>
<option selected="selected" value="FirstName">FirstName</option>
<option selected="selected" value="LastName">LastName</option>
<option selected="selected" value="Add1">Add1</option>
<option selected="selected" value="Add2">Add2</option>
<option selected="selected" value="Suburb">Suburb</option>
<option selected="selected" value="State">State</option>
<option selected="selected" value="Zip">Zip</option>
<option selected="selected" value="Team">Team</option>
<option selected="selected" value="Dteam">Dteam</option>
<option selected="selected" value="Fteam">Fteam</option>
<option selected="selected" value="DPID">DPID</option>
<option selected="selected" value="Error">Error</option>
<option selected="selected" value="Address1">Address1</option>
<option selected="selected" value="Address2">Address2</option>
<option selected="selected" value="correct3">correct3</option>
<option selected="selected" value="correct4">correct4</option>
<option selected="selected" value="correct5">correct5</option>
<option selected="selected" value="fcorrect">fcorrect</option>
<option selected="selected" value="bspprint">bspprint</option>
</select>
身份证件
BSPKey
印刷邮递
条形码
名字
姓氏
地址1
地址2
郊区
陈述
拉链
团队
数据团队
Fteam
DPID
错误
地址1
地址2
更正3
更正4
正确的5
F纠正
BSP打印
希望有人能找到一个简单的解决方案。您只需迭代最后得到的数组(
$col
)。只需在这个数组上使用foreach
,然后像这样填充HTML
<select name="column_name[]" multiple="multiple">
<?php
foreach($col as $key=>$val) {
?>
<option selected="selected" value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>
谢谢,伙计,它做得很好,如果它对你有帮助,请接受它作为正确的答案。你真的试过了吗?你被封锁在哪里?