首页 > 解决方案 > HTML 表未显示 PostgreSQL 的第一条记录

问题描述

我已经成功地用来自 PostgreSQL 的用户查询填充了一个 HTML 表,问题是当我直接在 PostgreSQL 数据库上进行查询时,它会根据查询显示完整的记录,但是当我在我的 PHP 中执行并填充时带有它的 HTML 不显示第一个

例如,我有 500 条记录,我希望它们按DESC顺序显示:

500
499
498
...

但它显示:

499
498
497
...

所以它总是“跳跃”第一条记录,我打电话给每个查询它会是什么?显然我需要所有的记录。

这是我用来运行查询的代码($sql1),$conexion 是到我的 postgresql 数据库的连接:

$rs  =odbc_exec($conexion, $sql1);
$nr  =odbc_result($rs, 1);
if(!empty($nr)){
    print "
        <table width='992' border='1' class='Gtable'>
        <tr>
        <td width='46' bgcolor='#EEEDE8'><strong>Codigo<strong</td>
        <td width='46' bgcolor='#EEEDE8'><strong>Matricula</strong></td>
        <td width='100' bgcolor='#EEEDE8'><strong>Municipio</strong></td>
        <td width='150' bgcolor='#EEEDE8'><strong>Barrio</strong></td>
        <td width='80' bgcolor='#EEEDE8'><strong>Concepto</strong></td>
        <td width='50' bgcolor='#EEEDE8'><strong>Tipo Susp.</strong></td>
        <td width='200' bgcolor='#EEEDE8'><strong>Observacion</strong></td>
        <td width='156' bgcolor='#EEEDE8'><strong>Cliente</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Direccion</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Telefono</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>F.Grabacion</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Planificacion</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Inicio</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Finalizacion</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Efectiva</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Descripcion de Cierre</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Contratista</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Empleado</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>F.Cierre</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Genero</strong></td>
        <td width='151' bgcolor='#EEEDE8'><strong>Cerro</strong></td>
        </tr>";

        while($result =odbc_fetch_array($rs)){
                print "<tr>
                    <td align='left' valign='top'>".$result['cod_otrb']."</td>
                    <td align='left' valign='top'>".$result['cod_pred']."</td>
                    <td align='left' valign='top'>".$result['descripcion']."</td>
                    <td align='left' valign='top'>".$result['brdesc']."</td>
                    <td align='left' valign='top'>".$result['tqdesc']."</td>
                    <td align='left' valign='top'>".$result['cod_ttrb']."</td>
                    <td align='left' valign='top'>".$result['spdesc']."</td>
                    <td align='left' valign='top'>".$result['prnombre']."</td>
                    <td align='left' valign='top'>".$result['direccion']."</td>
                    <td align='left' valign='top'>".$result['telefono']."</td>
                    <td align='left' valign='top'>".$result['fecha_grab']."</td>
                    <td align='left' valign='top'>".$result['fecha_pla']."</td>
                    <td align='left' valign='top'>".$result['fecha_ini_trb']."</td>
                    <td align='left' valign='top'>".$result['fecha_pre_cierre']."</td>
                    <td align='left' valign='top'>".$result['efectiva']."</td>
                    <td align='left' valign='top'>".$result['otdesc']."</td>
                    <td align='left' valign='top'>".$result['ctnombre']."</td>
                    <td align='left' valign='top'>".$result['emnombre']."</td>
                    <td align='left' valign='top'>".$result['fecha_cierre']."</td>
                    <td align='left' valign='top'>".$result['usr_genera']."</td>
                    <td align='left' valign='top'>".$result['usr_cierra']."</td>
                    ";

        }
        print"</table>";
}else{
    print "<font color='#CC3333'><strong>No Hay Registros</strong></font>";
}

标签: phphtmlpostgresql

解决方案


您的问题(正如@Sean 在评论中指出的那样)是这一行:

$nr =odbc_result($rs, 1);

正在从结果集中读取第一行,并且您没有从该行输出数据。要解决这个问题,我会推荐一个if ... do ... while结构:

if ($result = odbc_fetch_array($rs)) {
    // output the table headers here
    do {
        // output the row data
    } while ($result = odbc_fetch_array($rs));
}
else {
    // no data to output
}

推荐阅读