Re: Paging Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrei wrote:

    Since you query all enregs from table why not query all first and
the do mysql_data_seek on result?

 // Query to show
 $query_rsData = "SELECT * FROM {table} ORDER BY {Whatever field}";
 $rsData = mysql_query($query_rsData, $DB_CONECTION);
 $num_total_records = mysql_num_rows( $rsData );

 $total_pag = ceil($num_total_records / $NUM_RECORDS);

 if( $pag < 1 )
     $pag = 1;
 elseif( $pag > $total_pag )
     $pag = $total_pag;

 $start_enreg = ($pag-1) * $NUM_RECORDS;

 @mysql_data_seek( $rsData, $start_enreg );

 while( ($result = mysql_fetch_object( $rsData )) )
 {
    // display data...
 }


Querying all data in the first place is a waste, and the practice of seeking through a dataset seems silly when MySQL has the "LIMIT" feature to specify which data you want to return.

I believe the proper way to do this would be something like this (Simplified example, of course):

$query = "SELECT SQL_CALC_FOUND_FOWS * FROM foo LIMIT 30, 10";
$result = mysql_query($query);
$query = "SELECT found_rows()";
$num_rows = mysql_result(mysql_query($query), 0);

That should be much faster than the methods used by either of you guys.

As a comment, it would have been better to do "SELECT COUNT(*) FROM foo" and reading the result rather than doing "SELECT * FROM foo" and then mysql_num_rows(). Don't ask MySQL to collect data if you're not going to use it! That actually applies to why you shouldn't use mysql_data_seek either.

Regards, Adam.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux