Hola
Alejandr4, primero, deberìas encerrar tus còdigos entre las etiquetas [PHP ] y [/PHP ] (Sin espacios), para obtener un còdigo en colores y mas amigable al leer.
Bien, al ejecutar una consulta sql, el resultado queda guardado en una variable, en tu caso es la variable
$resultid. Luego, usualmente se usa una funcion while para recorrer los datos guardados en la variable. Me explico, las funciones mysq_fetch_
algo crearàn una matriz (mysql_fetch_
array, mysql_fetch_
assoc, etc.) o un objeto (mysql_fetch_
object) conteniendo los datos de la consulta, pero àètos arreglos no son como un simple
$datos = array();, son algo especiales. Al ejecutar por primera vez la funciòn, ésta devolverà el primer registro, luego al volver a usar la funcion, devolverà el segundo, y despues el tercero, y asi sucesivamente.
Imaginemos que haces una consulta que te devuelve 3 resultados, entonces colocar el siguiente còdigo:
Código PHP:
//$result = mysql_query($sql);
//En cada bucle, $datos va tomando los datos del siguiente registro
//Al llegar al ultimo registro, la funcion devuelve false y el bucle se detiene
while($datos = mysql_fetch_object($result)) {
echo $datos->Nombre."<br />\n";
}
es como que si colocaras:
Código PHP:
//$result = mysql_query($sql);
//mysql_fetch_object() devuelve el primer resultado
$datos = mysql_fetch_object($result);
echo $datos->Nombre."<br />\n";
//mysql_fetch_object() devuelve el segundo resultado
$datos = mysql_fetch_object($result);
echo $datos->Nombre."<br />\n";
//mysql_fetch_object() devuelve el tercer resultado
$datos = mysql_fetch_object($result);
echo $datos->Nombre."<br />\n";
Ahora, que pasaria si colocas un if antes del bucle, como lo estas haciendo:
Código PHP:
//$result = mysql_query($sql);
if($datos = mysql_fetch_object($result)) { //Devuelve el primer resultado
//En cada bucle, $datos va tomando los datos del siguiente registro, PERO
//ESTAVEZ ESTA EMPEZANDO DEL SEGUNDO REGISTRO, YA QUE EN EL IF
//LA FUNCION DEVOLVIO EL PRIMER REGISTRO PERO NO FUE USADO....
//Al llegar al ultimo registro, la funcion devuelve false y el bucle se detiene
while($datos = mysql_fetch_object($result)) {
echo $datos->Nombre."<br />\n";
}
}
Asì, el bucle se ejecuta solo con los dos ultimos registros... Para solucionar esto puedes usar en el if alguna funcion diferente, como mysql_num_rows(), que te devuelve el numero de filas que se obtuvo de la consulta, algo asi:
Código PHP:
$num = mysql_num_rows($result);
if($num >= 1) {
//[...]Código[...]
while($datos = mysql_fetch_object($result)) {
//[...]Más código[...]
}
} else {
echo "No hay resultados";
}
y tu código finalmente quedarìa:
Código PHP:
<?
$conn = mysql_connect("lldb499.servidoresdns.net","****"," *****") or die ('Lo sentimos, no se pudo establecer la conexión');
mysql_select_db("qbr261",$conn) or die ('Lo sentimos, no se pudo establecer la conexión');
$ssql = "select * from adopta";
$resultid = mysql_query($ssql,$conn) or die ("Fallo en la consulta");
//Cambié esta linea
if (mysql_num_rows($resultid) >= 1) {
?>
<td bgcolor="#F5F5FF">
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td><marquee id="marquee1" onMouseOver="marquee1.stop();" onMouseOut="marquee1.start()" direction="up" scrollAmount="1.7" height="150">
<table width="100%" height="100" border="0" align="center" cellpadding="2" cellspacing="0">
<?
while ($linea = mysql_fetch_object($resultid)){
?>
<tr>
<td width="140" rowspan="2" align="center" valign="middle"><a href="urgentes.php" target="contenido" onMouseOver="window.status='<? echo $linea->nombre;?>';return true" onMouseOut="window.status=' '">
<img src="<? echo $linea->imagen_p;?>" alt="<? echo $linea->nombre;?>" border="0"></a></td>
<td align="left"><a href="urgentes.php" class="sexto" target="contenido" onmouseover="window.status='<? echo $linea->nombre;?>';return true" onmouseout="window.status=' '"><? echo $linea->nombre;?></a></td>
</tr>
<tr>
<td align="left"><a href="urgentes.php" target="contenido" onMouseOut="window.status=' '" onMouseOver="window.status='<? echo $linea->nombre;?>';return true"><? echo $linea->entrada;?></a></td>
</tr>
<tr height="1">
<td height="20" colspan="2" align="center"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="1" bgcolor="#CC9933"><img src="../imag/pix.gif" width="1" height="1"></td>
</tr>
</table></td>
</tr>
<?php
$num_filas++;
}
?>
</table>
</marquee></td>
</tr>
</table>
</td>
<?
}
else{
?>
<td bgcolor="#F5F5FF"> </td>
<?php
}
mysql_free_result($resultid);
mysql_close($conn);
?>
Suerte. Un saludo,
P.D. ¿Acaso no se ve mejor el codigo coloreado
?