BD Results Count for Paging

0

I have a problem, I want to know how much data there is in the database, but with this code is returning -1 already used this code for other jobs (only with mysql), in this case is with SQL Server

$pag = (isset($_GET['pag'])) ? strip_tags((int)$_GET['pag']) : '1';

                                $maximo = '6';//produtos por pagina

                                $stmt2 = $conn->prepare("SELECT * FROM cl");
                                $stmt2->execute();
                                $totalRegistros2 = $stmt2->rowCount();
                                $paginas2 = ceil($totalRegistros2/$maximo);

                                if(isset($_GET['pag'])){
                                    if (($_GET['pag']>$paginas2)||($_GET['pag']<1)){
                                        $pag=1;
                                    }
                                }

                                $inicio = ($pag * $maximo) - $maximo;
                        echo $totalRegistros2;
                        echo $count;
    
asked by anonymous 26.05.2015 / 15:50

3 answers

0

Here you go with PDO right

$stmt2 = $conn->prepare("SELECT count(*) AS total_records FROM cl");
$stmt2->execute(); 
$total_records = $stmt2->fetch(PDO::FETCH_ASSOC)['total_records'];
    
26.05.2015 / 16:14
1

From the PHP documentation regarding the rowCount ()

  

If the last SQL statement executed by the PDOStatement was   a SELECT statement, some databases may return the number of rows   returned by that statement. However, this behavior is not guaranteed   for all databases and should not be relied on for portable   applications.

If the last statement is a SELECT, rowCount () is not guaranteed to return the number of records. It depends on the DBMS.

If you want to know the total number of records you can always do

$sql = "SELECT count(*) AS total_records FROM cl";
$rs_result = $db_connection->query($sql);
$total_records = $rs_result->fetch_assoc()['total_records'];
    
26.05.2015 / 15:54
0

You can use SELECT count(*) FROM cl to know how many rows / records this table has.

    
26.05.2015 / 15:55