Foros del Web » Programando para Internet » PHP »

Primer registro no aparece

Estas en el tema de Primer registro no aparece en el foro de PHP en Foros del Web. Haber si puede alguien echarme un guante, hago una consulta a la base de datos y dos bucles para que me aparezcan las preguntas y ...
  #1 (permalink)  
Antiguo 29/11/2005, 02:29
 
Fecha de Ingreso: octubre-2005
Mensajes: 151
Antigüedad: 18 años, 5 meses
Puntos: 1
Primer registro no aparece

Haber si puede alguien echarme un guante, hago una consulta a la base de datos y dos bucles para que me aparezcan las preguntas y las respuestas y el primer registro desaparece, el codigo es:

<?
//no puede ninguna línea vacía al principio
//Incluyo el mecanismo de control
$URL = $_SERVER['PHP_SELF'];
include "../../admin/controlSession.php";
?>
<?php require_once('../../Connections/conect.php'); ?>
<?php
$cod_tema=$_GET['cod_tema'];
$cod_curso=$_GET['cod_curso'];
$num_test=$_GET['num_test'];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #6699FF;
}
.Estilo5 {
font-family: Arial, Helvetica, sans-serif;
color: #00FFFF;
}
.Estilo6 {
color: #FFFFFF;
font-size: 14px;
}
.Estilo7 {
color: #FF0000;
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
}
a {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #FF0000;
}
a:active {
color: #FFFFFF;
}
.Estilo10 {font-family: Arial, Helvetica, sans-serif; color: #6699FF; }
.Estilo11 {color: #FFFFFF}
-->
</style></head>

<body>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">


<tr>
<td colspan="3"><table width="770" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr>
<td><div align="center"><img src="../images/portada/banner.jpg" width="770" height="100"></div></td>
</tr>
</table></td>
</tr>
<tr>
<td><a href="javascript:history.go(-1);">volver</a></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td valign="top"><?php echo $totalpreguntas ?></td>
<td valign="top"><table width="500" border="1" align="center" cellpadding="10" cellspacing="0" bordercolor="#FFFFFF">
<tr>
<td><span class="Estilo7"></span></td>
</tr>
<tr>
<td><div align="center">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<?php
mysql_select_db($database_conect, $conect);
$query_preguntas = "SELECT pregunta,cod_pregunta FROM db_listado_test_preguntas WHERE cod_tema='".$cod_tema."' and cod_curso='".$cod_curso."' and num_test='".$num_test."'";
$preguntas = mysql_query($query_preguntas, $conect) or die(mysql_error());
$row_preguntas = mysql_fetch_assoc($preguntas);
$totalpreguntas = mysql_num_rows($preguntas);

if($totalpreguntas>0){
while(list($pregunta,$cod_pregunta)=mysql_fetch_ar ray($preguntas)){


echo"<tr><td><div align='left'>$pregunta</div></td> </tr>";



$query_respuestas = "SELECT respuesta,cod_respuesta FROM db_listado_test_respuestas WHERE cod_pregunta = $cod_pregunta";
$respuestas = mysql_query($query_respuestas, $conect) or die(mysql_error());
$row_respuestas = mysql_fetch_assoc($respuestas);
$totalrespuestas = mysql_num_rows($respuestas);

if($totalrespuestas>0){
while(list($respuesta,$cod_respuesta)=mysql_fetch_ array($respuestas)){


echo"<tr><td><div align='left'>$respuesta</div></td> </tr>";
}
}

}

}
?>



<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<tr>
<td></td>
</tr>
</table>
</div> </td>
</tr>
</table></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<div align="center" class="Estilo11">Copyright &copy; 1995-2004 Grupo sie. Todos los derechos reservados. Pol&iacute;tica de privacidad.</div>
</body>
</html>
<?php
mysql_free_result($respuestas);

mysql_free_result($preguntas);
?>
  #2 (permalink)  
Antiguo 29/11/2005, 02:44
Avatar de Tx
Tx
 
Fecha de Ingreso: enero-2002
Ubicación: Cba - Arg
Mensajes: 188
Antigüedad: 22 años, 2 meses
Puntos: 1
Hola,
segun lo poco que puedo ver, el error estaria mas o menos aca:
Código PHP:
 mysql_select_db($database_conect$conect);
$query_preguntas "SELECT pregunta,cod_pregunta FROM db_listado_test_preguntas WHERE cod_tema='".$cod_tema."' and cod_curso='".$cod_curso."' and num_test='".$num_test."'";
$preguntas mysql_query($query_preguntas$conect) or die(mysql_error()); // (1)
$row_preguntas mysql_fetch_assoc($preguntas); // (2)
$totalpreguntas mysql_num_rows($preguntas);

if(
$totalpreguntas>0){
while(list(
$pregunta,$cod_pregunta)=mysql_fetch_ar ray($preguntas)){ // (3)


echo"<tr><td><div align='left'>$pregunta</div></td> </tr>"
en el punto (1) haces la consulta, en el punto (2) te posicionas en el primer registro extrayendo los datos, y luego en el punto (3) empiezas el bucle extrayendo los datos, pero como estabas posicionado en el primer registro (punto 2), el bucle empieza desde el segundo registro.
Lo mismo haces en el bucle de abajo.
Me entendes?, bueno, cualquier cosa lo volvemos a ver.

Saludos...

PD: estaria bueniiiisimo que le pongas etiquetas PHP el codigo que pegues, se lee mejor
  #3 (permalink)  
Antiguo 29/11/2005, 06:24
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 2 meses
Puntos: 129
En resumen .. te sobra el:

$row_preguntas = mysql_fetch_assoc($preguntas);

Justamente por lo que han comentado .. Las funciones "fetch" te entregan el primer registro de un "record-set" y mueven el puntero al siguiente resultado ..

Un saludo,
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:58.