Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to get total rows and 10 row for display by one select statement mysql?.
Old 10-01-2010, 06:53 AM How to get total rows and 10 row for display by one select statement mysql?.
Novice Talker

Posts: 7
Name: Nguyen Van Thu
Location: Viet Nam
Trades: 0
Hello every.
I have table news on database mysql.
When display list news on page, I must get total rows for paging.

After, I often use 2 statement mysql for on task, but I see a php coder guru only use a statement mysql.

Please help me use one statement for this solve.
__________________

Please login or register to view this content. Registration is FREE
ngockhoa is offline
Reply With Quote
View Public Profile Visit ngockhoa's homepage!
 
 
Register now for full access!
Old 10-01-2010, 07:16 AM Re: How to get total rows and 10 row for display by one select statement mysql?.
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Use mysql_num_rows() after SELECT query.
miki86 is online now
Reply With Quote
View Public Profile
 
Old 10-01-2010, 07:32 AM Re: How to get total rows and 10 row for display by one select statement mysql?.
Novice Talker

Posts: 7
Name: Nguyen Van Thu
Location: Viet Nam
Trades: 0
Thank for reply.
__________________

Please login or register to view this content. Registration is FREE
ngockhoa is offline
Reply With Quote
View Public Profile Visit ngockhoa's homepage!
 
Old 10-01-2010, 10:00 AM Re: How to get total rows and 10 row for display by one select statement mysql?.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
The proper way to do this is with 2 queries:
PHP Code:
$page 1;
$perPage 10;
$start = ($page 1) * $perPage 1;
$where " WHERE condition = 'value'";

$countSql "SELECT COUNT(1) as num_records FROM my_table {$where}";
$resultSql "SELECT * FROM my_table {$where} LIMIT {$start}{$perPage}";

$count mysql_query($countSql);
$count mysql_fetch_assoc($count);
$total $count["num_records"];  // Total number of records in your table

$results mysql_query($resultSql);

$displayCount mysql_num_rows($results);  // 
$end $start $displayCount;

$displayText "Displaying {$start} to {$end} of {$total} results."
The first query will return the total number of records matching your WHERE clause, and the second will return 10 records on the page specified.

If you use only the 2nd query without a limit, you'll retrieve all the data from your database (could be thousands or even millions of records), which will put an unnecessary load on the DB.

The above is the proper solution for maximum performance.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 10-01-2010 at 10:04 AM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-01-2010, 11:43 PM Re: How to get total rows and 10 row for display by one select statement mysql?.
Novice Talker

Posts: 7
Name: Nguyen Van Thu
Location: Viet Nam
Trades: 0
Other way to solve.

PHP Code:
$sql="SELECT SQL_CALC_FOUND_ROWS {$this->listField} FROM ".$this->tableName." WHERE {$condition} ORDER BY {$order} LIMIT {$limit}";
            
$db->query($sql);//get list records to display
$result=$db->fetchAll($key);
//get total row effect
$rows $db->query("SELECT FOUND_ROWS() AS total_rows");
$total_row = (int)$rows[0]['total_rows']; 
__________________

Please login or register to view this content. Registration is FREE
ngockhoa is offline
Reply With Quote
View Public Profile Visit ngockhoa's homepage!
 
Old 10-02-2010, 01:34 AM Re: How to get total rows and 10 row for display by one select statement mysql?.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
SQL_CALC_FOUND_ROWS is often (usually?) slower than running a separate limit query and count. And by the way, it doesn't simplify anything on the PHP side... you still have to process 2 SQL queries.

Edit: another point...
SQL_CALC_FOUND_ROWS is MySQL specific... best to get in the habit of writing SQL code that is as cross-DB compliant as possible... if you've ever had to convert from one engine to another, or write an app that would be iinstallable on multiple DB types, you know what I'm talking about.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 10-02-2010 at 01:43 AM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-02-2010, 03:18 PM Re: How to get total rows and 10 row for display by one select statement mysql?.
Junior Talker

Posts: 3
Name: Miro
Trades: 0
Generally - you are right, but not 100% of all cases. It depends on how frequently are queried table updated. More info here

About what could be simplified - a lot. Example, your application DB abstract layer could 'listen' for 'SQL_CALC_FOUND_ROWS' in select query and execute auto-magically 'SELECT FOUND_ROWS()' (and store the total result).

Quick example:
PHP Code:
$this->total_results false;
if ((
strpos($query,'SQL_CALC_FOUND_ROWS') !== FALSE) && (strpos($query,'SELECT') !== FALSE))
{    
// Need to get the total record count as well. Return code is a resource identifier
    // Have to do this before any debug action, otherwise this bit gets messed up
    
$fr mysql_query('SELECT FOUND_ROWS()'$this->mySQLaccess);
    
$rc mysql_fetch_array($fr);
    
$this->total_results = (int) $rc['FOUND_ROWS()'];

__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
secretr is offline
Reply With Quote
View Public Profile Visit secretr's homepage!
 
Old 10-03-2010, 09:47 PM Re: How to get total rows and 10 row for display by one select statement mysql?.
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
You could do the same with a COUNT query. However, you could use SQL_CALC_FOUND_ROWS in SQL without having to set parameters in a query or stored procedure, for example:

Code:
SELECT SQL_CALC_FOUND_ROWS, * FROM my_table;
INSERT INTO my_table_results (rows, search_date) VALUES (FOUND_ROWS(), CURRENT_TIMESTAMP);
You could theoretically run this in one query using mysqli, which would return your single resultset, meaning you'd never have to bother working with the row count.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 10-03-2010 at 09:50 PM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Reply     « Reply to How to get total rows and 10 row for display by one select statement mysql?.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27107 seconds with 12 queries