# PHP mysql Pagination

**URL:** <https://www.phphelp.com/t/php-mysql-pagination/13244>\
**Category:** General PHP Help\
**Created:** [May 4, 2011, 12:19pm UTC](https://www.phphelp.com/t/php-mysql-pagination/13244 "2011-05-04T12:19:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mithrandix](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@mithrandix](https://www.phphelp.com/u/mithrandix)\
**Post date:** [May 4, 2011, 12:19pm UTC](https://www.phphelp.com/t/php-mysql-pagination/13244/1 "2011-05-04T12:19:19Z")

</div>

Hello, i’m new around here.

I have a query, and when I click on the Next button to show my other results.

When I 1º do the display, it shows 6 results that are correct and after I do next, no error are displayed but the displayed data go from 6 to 7 :S

Can I get a help on this please =S rly impp…  
[php]\<?php

$host = “localhost”;  
$username = “root”;  
$senha= “”;  
$banco\_dados = “\*\*\*\*\*\*\*”;  
$tabela = “artigos”;  
$numero\_links = “4”;  
$total\_reg = “2”;

if(isset($\_POST[“Familia”])){  
$search = mysql\_escape\_string($\_POST[“Familia”]);  
} else {  
$search = “”;  
}

if(!$pagina) {  
$pc = “1”;  
} else {  
$pc = $pagina;  
}

$intervalo = $numero\_links;  
$inicio = $pc-1;  
$inicio = $inicio\*$total\_reg;  
mysql\_connect($host,$username,$senha);  
mysql\_select\_db($banco\_dados);

$sql = mysql\_query(“SELECT \* FROM $tabela where Familia LIKE '%”.$search."%’ “);  
$tr = mysql\_num\_rows($sql);  
$sql2 = mysql\_query(“SELECT \* FROM $tabela where Familia LIKE '%”.$search.”%’ LIMIT $inicio,$total\_reg");  
$tp = ceil($tr/$total\_reg);

echo “”;  
echo “Página $pc de $tp  
 Total de registros: $tr  
  
”;  
echo “”;  
echo “

* * *
”;  
while($dados=mysql\_fetch\_array($sql2)) {  
$id = $dados[“Nome”];  
$nome = $dados[“CodigoBarras”];  
echo “$id - $nome  
\n”;  
}

$aux = $tp/$intervalo;  
$aux1 = $pc/$intervalo;  
$pi = $aux1 \* $intervalo;  
if ($pi == “0”) {  
$pi = “1”;  
}  
$pf = $pi + $intervalo -1;  
$anterior = $pi-$intervalo;  
if($pc\<=$intervalo) {  
$anterior = 1;  
}  
$aux2 = $pi + 1;  
if($pi\>1) {  
$aux = $pi - 1;  
$aux2 = $pi + 1;  
echo “

* * *
”;  
echo " \<\<\<a href=’{$\_SERVER[‘PHP\_SELF’]}?pagina=1’\>Primeiro ";  
echo "\< **Anterior** &nbsp;";  
}  
else  
{  
echo “”;  
echo "\<\< Primeiro &nbsp;";  
echo “”;  
echo “”;  
echo "\<\< Anterior &nbsp;&nbsp;";  
echo “”;  
}

for ($pi;$pi\<$pf;$pi++)  
{  
if($pi\<=$tp) {  
if($pc==$pi) {  
echo “**”;  
echo “[” . $pi . "]&nbsp;";  
echo “**”;  
} else {  
echo “” . $pi . "&nbsp;";  
}  
}  
}

if($pc != $tp){  
echo “**”;  
echo "&nbsp;&nbsp;&nbsp; Próximo \>";  
echo " \<a href=’{$\_SERVER[‘PHP\_SELF’]}?pagina=$tp’\>Ultimo “;  
echo “**”;  
}  
else  
{  
echo “”;  
echo "&nbsp;&nbsp;&nbsp; Próximo \>\>”;  
echo “”;  
echo “”;  
echo "&nbsp;&nbsp;&nbsp; Ultimo \>\>";  
echo “”;  
}  
?\>  
[/php]
