-1
I changed the method of connecting my page to my database via PDO. I used until then a pagination code only that is now returning the error:
Warning: mysql_num_rows() expects Parameter 1 to be Resource, string Given in D: xampp htdocs n_archaeus inc index.php on line 85
My code is like this:
$pg = isset($_GET['pg'])?$_GET['pg']:"1";
$quantidade = 3;
$ini = ($pg*$quantidade) - $quantidade;
$qry = "
SELECT
content.id_content,
content.img,
content.titulo,
povos.pv,
cat.categoria,
content.inicio,
content.fim,
content.content,
regiao.reg,
regiao.wmap
FROM cat
INNER JOIN regiao
INNER JOIN povos
INNER JOIN content ON povos.id_povos = content.civ
AND regiao.id_regiao = povos.regiao
AND cat.id_cat = content.clas
ORDER BY inicio
LIMIT $ini, $quantidade";
$resultado = $PDO->query( $qry );
$rows = $resultado->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $key => $linha) {
$civ = $linha['pv'];
$clas = $linha['categoria'];
$inicio = $linha['inicio'];
$fim = $linha['fim'];
$titulo = $linha['titulo'];
$conteudo = $linha['content'];
$imagem = $linha['img'];
$reg = $linha['reg'];
$wmap = $linha['wmap'];
echo $civ; etc. etc. etc...
}
$sql_2 = "SELECT * FROM content ORDER BY inicio ";
$res_2 = $PDO->query( $sql_2 );
$row_2 = $res_2->fetchAll(PDO::FETCH_ASSOC);
$total_registros = mysql_num_rows($sql_2);
$paginas = ceil($total_registros/$quantidade);
$links = '9';
echo "<br><center><p class='paginas'><a href='?id=37&pg=1'>Primeira Página </a> ";
for($i = $pg-$links; $i <= $pg-1; $i++){
if($i<=0){
}else{
echo " <a href='?id=37&pg=".$i."'><strong>".$i."</strong></a> ";
}
}
echo "<a href=#> [ $pg ] </a>";
for($i = $pg+1; $i <= $pg+$links; $i++){
if($i>$paginas){
}else{
echo " <a href='?id=37&pg=".$i."'>".$i."</a> ";
}
}
echo " <a href='?id=37&pg=".$paginas."'>Última Página </a></p></center> ";
?>
I think in
mysql_num_rows($sql_2)
should bemysql_num_rows($res_2)
, right? Otherwise, you should use$res_2->rowCount()
which is most recommended when using a PDO connection. http://php.net/manual/en/pdostatement.rowcount.php– Caio Guedes
Hi Caio. Thank you for the reply, but I still could not make this consultation work. My code looks like this $sql_2 = "SELECT * FROM content ORDER BY start "; $res_2 = $PDO->query( $sql_2 ); $row_2 = $res_2->fetchAll(PDO::FETCH_ASSOC); $total_records -> row_count($res_2); $pages = Ceil($total_records/$quantity); and the error message looks like this: "Notice: Undefined variable: total_registros in D: xampp htdocs n_archaeus inc index.php on line 86 Fatal error: Call to a Member Function row_count() on a non-object in D: xampp htdocs n_archaeus inc index.php on line
– Webster Moitinho
You need to call
rowCount()
ofStatement
which is the variable use$res_2
. In this case you must call$total_registros = $res_2->rowCount()
to have the total records selected.– Caio Guedes
Thanks Caio, now it works
– Webster Moitinho