【问题标题】:HTML Table not showing first record of PostgreSQLHTML 表未显示 PostgreSQL 的第一条记录
【发布时间】:2020-04-06 07:47:19
【问题描述】:

我已经成功地用来自 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>";
}

【问题讨论】:

  • 记录索引是否从零开始(从零开始)?如果是这样,您将获得 0 - 499(500 条记录)。
  • 虽然我不熟悉 PostgreSQL/odbc_*,但我的猜测是$nr =odbc_result($rs, 1); 正在拉第一条记录(500),并将内部指针移动到下一条。因此,当您尝试循环结果时,它位于第二条记录处。
  • 您可能需要更改该行以改用odbc_num_rows()
  • @Sean odbc_num_rows() 不能保证在SELECT 查询中返回可用的结果。

标签: php html postgresql


【解决方案1】:

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

$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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多