显示列查询显示列数据类型而不是列值

时间:2018-03-09 20:40:17

标签: php mysqli mariadb

我正在尝试使用查询:

SHOW COLUMNS FROM @TABLENAME.DB

在html标签内,如:

<input type="text" name="colName" value="<?php echo $row[1]; ?>">

echo语句的输出显示“数据类型”列,例如ex - INT(11)。

然而,当我在一个简单的php文件中使用相同的查询时,输出 $ row [n]是列名。

为什么?

2 个答案:

答案 0 :(得分:1)

即使表的列发生更改,结果的列名也来自查询SHOW COLUMNS FROM table。但是,您可以使用mysqli_fetch_array()来获取两者:

$rs = mysqli_query($con, "SHOW COLUMNS FROM employee");
while ($row = mysqli_fetch_array($rs)) {
    // print_r($row) ;
    // echo $row['Field'] ; // Name of table column.
    echo $row['Type'] ;
}

(最后)编辑

//1. get columns.

$cols = [] ;
$rs = mysqli_query($con, "SHOW COLUMNS FROM employee");
while ($row = mysqli_fetch_assoc($rs)) {
    $cols[] = $row['Field'];
}

//2. get values :

$rs = mysqli_query($con, "SELECT * FROM employee");
while($rows = mysqli_fetch_array($rs)){

    //leftside cols
    $pdf->SetFontSize(10);
    $pdf->SetXY(10,50);
    $pdf->Cell(50,10,$cols[0]. ":",0,0,'L');
    $pdf->Cell(30,10,$code,0,1,'C');

    $pdf->Cell(50,10,$cols[1]. ":",0,0,'L');
    $pdf->Cell(30,10,$designation,0,1,'C');

    $pdf->Cell(50,10,$rows[0]. ":",0,0,'L');
    $pdf->Cell(30,10,$department,0,1,'C');
}

答案 1 :(得分:0)

这是最终的工作代码:

$rs = mysqli_query($con, "SHOW COLUMNS FROM employee");
$cols = array();
while($rows = mysqli_fetch_row($rs)){
    $cols[] = $rows[0];
}
//leftside cols
    $pdf->SetFontSize(10);
$pdf->SetXY(10,50);
$pdf->Cell(50,10,$cols[1]. ":",0,0,'L');
$pdf->Cell(30,10,$code,0,1,'C');

$pdf->Cell(50,10,$cols[4]. ":",0,0,'L');
$pdf->Cell(30,10,$designation,0,1,'C');

$pdf->Cell(50,10,$cols[5]. ":",0,0,'L');
$pdf->Cell(30,10,$department,0,1,'C');

$pdf->Cell(50,10,"@".$cols[3]. ":",0,0,'L');
$pdf->Cell(30,10,$email,0,1,'C');

...........