Using mysql_num_rows with PDO

Asked

Viewed 1,279 times

-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&aacute;gina </a>&nbsp;&nbsp;";

for($i = $pg-$links; $i <= $pg-1; $i++){
        if($i<=0){
            }else{
                echo "&nbsp;&nbsp;<a href='?id=37&pg=".$i."'><strong>".$i."</strong></a>&nbsp;&nbsp;";
                }
    }

    echo "<a href=#> [ $pg ] </a>";

for($i = $pg+1; $i <= $pg+$links; $i++){
        if($i>$paginas){
            }else{
                echo "&nbsp;&nbsp;<a href='?id=37&pg=".$i."'>".$i."</a>&nbsp;&nbsp;";
                }
    }

echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='?id=37&pg=".$paginas."'>&Uacute;ltima P&aacute;gina </a></p></center>&nbsp;&nbsp;";
?>
  • 1

    I think in mysql_num_rows($sql_2) should be mysql_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

  • 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

  • 2

    You need to call rowCount() of Statement which is the variable use $res_2. In this case you must call $total_registros = $res_2->rowCount() to have the total records selected.

  • Thanks Caio, now it works

1 answer

3


It was necessary to call rowCount() of Statement which is the variable use $res_2. In this case it was called

$total_registros = $res_2->rowCount()

To have the total of records selected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.