如何使用带索引的PDO :: FETCH_COLUMN显示列名?

时间:2017-03-24 05:37:46

标签: php mysql pdo

//提交的PHP代码

include("cfg.php");
if($_SERVER["REQUEST_METHOD"]=="POST"){

$tbname = $_POST['c'];
$last = $_POST['dbTables'];

$result = $db->prepare("ALTER TABLE `cont` ADD $tbname DECIMAL(20,2) NOT NULL DEFAULT '0.00' AFTER $last");
$result->execute(); 
unset($_POST);
header('Location: '.$_SERVER['PHP_SELF']);

// BODY

<form name="f1" method="post" action="">
<table>
<tr><td>Last Column<td></tr>
<tr>
<td>
<select id="dbTables" name="dbTables">

<?PHP
    $rs = $db->query('DESCRIBE cont');
    $rs->execute();

    $table_fields = $rs->fetchAll(PDO::FETCH_COLUMN);
    foreach ($table_fields as $row){


        echo '<option value="'.$row.'" selected="selected">'.$row.'</option>';


    }
?>


</select>
</td>
<td><input type="text" name="c" placeholder="db table name"></td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>

<table name="tb3">

<th>Date</th>
<th>Alobs</th>
<th>Claimant</th>
<th>Description</th>
<?PHP

$result= $db->query('DESCRIBE cont');
$result->execute();

$fieldname = $result->fetchAll(PDO::FETCH_COLUMN);
foreach($fieldname as $row){

    echo '<th>'.$row.'</th>';
}


?>


</table>

有没有办法排除显示带黄色突出显示的那些?我一直在网上寻找答案,但无济于事。希望有人帮忙。 https://i.stack.imgur.com/0EJVC.png

1 个答案:

答案 0 :(得分:0)

我必须承认整个代码对我没有意义。我也不能在问题标题和正文之间建立任何联系。但是,从字面上取名称

foreach($fieldname $index as $row){

    echo "<th>$index. $row</th>";
}