Jump to content

Help with this code paging


zazata

Recommended Posts

I'm starting to practice in PHP programming. I need to paginate the result of a query. I used the tutorial from this site but I get the following error:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\rejuhu_php\procesa_busqueda_pag.php on line 97

 

the highlighting in red is line 97 where the error flag. Someone could help me?. Thank you very much.

 

Below is the complete code

 

<?php

//***********************************************************************************************************

$tipobusqueda = $_POST['tipobusqueda'];

$terminobusqueda = $_POST['terminobusqueda'];

$conexion=mysql_connect("localhost","root","desarrollo") or die("Problemas en la conexion");

mysql_select_db("rejuhu_php",$conexion) or die("Problemas en la selección de la base de datos");

 

//Ver que cantidad de registros tiene la tabla personas

$sql = "Select count(*) from personas";

$result = mysql_query($sql, $conexion) or die("Problemas en el selector:".mysql_error());

$r = mysql_fetch_row($result);

$numrows = $r[0];

 

//Número de registros por página

$rowsperpage = 30;

 

//Averiguar el total de páginas encontradas

$totalpages = ceil($numrows / $rowsperpage);

 

//obtener la página actual o establecer un valor predeterminado

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {

 

//var emitidos como int

$currentpage = (int) $_GET['currentpage'];

} else {

// Número de página predeterminada

$currentpage = 1;

}// end if

// si la página actual es mayor que el total de páginas

if ($currentpage > $totalpages) {

// configuración de la página actual a la última página

$currentpage = $totalpages;

} //end if

// si la página actual es inferior a la primera página

if ($currentpage < 1) {

// configuración de la página actual a la primera página

$currentpage = 1;

}//end if

// el desplazamiento de la lista, basada en la página actual

$offset = ($currentpage - 1) * $rowsperpage;

//obtener la información de la BD

$sql = "select * from personas LIMIT $offset, $rowsperpage where $tipobusqueda like '$terminobusqueda%'";

$result = mysql_query($sql, $conexion);

//mientras haya filas que se van a buscar

 

while($reg = mysql_fetch_assoc($result))   { //aquí da el error

//Imprimir datos           

echo "<tr><td>".$reg['PrimerNombre']."</td><td>".$reg['SegundoNombre']."</td><td>".$reg['TercerNombre']."</td><td>".$reg['PrimerApe']."</td><td>".$reg['SegundoApe']."</td><td>".$reg['ApellidoCasada']."</td><td>".$reg['NroDocumento']."</td><td>".$reg['NroTelefono']."</td><td>".$reg['Direccion']."</td></tr>";

} //end while

echo "</table>";

 

/*Construyendo la paginación */

//num serie de enlaces para mostrar

$range = 3;

// Si no está en la página 1, no mostrar "para atrás"

if ($currentpage > 1) {

// Mostrar << link para pág. 1

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";

//obtener num la página anterior

$prevpage = $currentpage -1;

// Mostrar < para volver a pág. 1

echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";

} //end if

//bucle para mostrar los vínculos a la gama de páginas a la página actual

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {

// si se trata de un número de página válido

if (($x > 0) && ($x <= $totalpages)) {

//si estamos en la página actual

if ($x == $currentpage) {

//resaltarlo, pero no hacer un enlace

echo " [<b>$x</b>] ";

//si no es la pág. actual

} else {

//hacer enlaces

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";

}// end else

}// end if

}// end for

// si no en la última página, muestran hacia adelante y la última página enlaces

if ($currentpage != $totalpages) {

// obtener la próxima pág.

$nextpage = $currentpage + 1;

//echo forward link for next page

echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";

//echo forward link for last page

echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpage'>></a> ";

}

/*Fin construcción paginación*/

?>

Link to comment
https://forums.phpfreaks.com/topic/182203-help-with-this-code-paging/
Share on other sites

Hello zazata,

Your SQL is wrong and so $result is the value false. You should always check to make sure the SQL actually works.

 

Put the LIMIT statement at the end and try it.

$sql = "select * from personas LIMIT $offset, $rowsperpage where $tipobusqueda like '$terminobusqueda%'";

 

Also, you can use this:

if (!$result) {
    // error!
}

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.