php - 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>";
}
解决方案
您的问题(正如@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
}
推荐阅读
- tensorflow - 为什么使用tensorflow做物体检测时Average Precision和Average Recall = -1?
- swift - SwiftUI 不正确的帧大小
- android - 有什么想法可以提高此自定义视图的渲染性能吗?
- python - 库贝特尔。服务中找不到环境变量
- heroku - Stripe 客户门户、后端服务器和移动设备
- powershell - 在 PowerShell 中对对象中的多个属性进行排序
- python - 处理 tictactoe 的异常情况
- docker - 找不到任何实现 Connector 且名称与 MySQL 匹配的类
- c# - 如何使用取消令牌中止线程?
- java - 无法找出此 javaFXML 文件中的错误