Solved

divide into pages

Posted on 2000-04-11
7
198 Views
Last Modified: 2010-03-05
i want to write a PERL CGI program that can show 10 records each pages of my database.
i use DBI and mysql.

i would like to show the page numbers in every pages so that a user can click for example 2, then bring him to page2.

i want that the number of pages are automatically calculated in the program.

how to implement this?

how can i get the number of records in my database?
using this value, i can divide it by 10 so that i can get the number of pages.

any one help me?

thanks

Stanley

0
Comment
Question by:stanleyhuen
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
olthoff earned 30 total points
Comment Utility
select count(1) from TABLE; will give you a count of all of the records in the table.

Then to limit your output you do:

select * from TABLE where ? = ? LIMIT 10;

This will limit tour output to 10 rows.  Then you have to create the query so you get the data you want.  I am not sure what it is in MYSQL but probably something like:

select * from TABLE where ROW_ID > ? LIMIT 10;

This will give you your page #.  ? would be Page # * 10
0
 

Author Comment

by:stanleyhuen
Comment Utility
thanks olthoff,

though i now knowselect count(1) from TABLE; will give me a count of all of the records in the table.

but how to get this number with DBI?
i only know to use
$sth->fetchrow_hashref;

as it will only return a number,
how to get it?

0
 

Author Comment

by:stanleyhuen
Comment Utility
what is count(1) mean?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:olthoff
Comment Utility
It counts the number of records, I believe that the (1) is the first field in the table, it really doesn't matter because you want a count.  It is similar to

select count(name) from customer;

as for how to use the DBI, I would suggest reading the documentation, but a simple example (from memory) would be

$db = DBI->connect(db:MYSQL, name, password);
$db->prepare("select count(1) from table");
$db->bind_columns(\$Count);
$db->execute;
#This will get the count into the $Count variable;
$db->fetch;
$db->disconnect;

You can do your other query like this as well.


 


0
 

Author Comment

by:stanleyhuen
Comment Utility
Adjusted points from 13 to 30
0
 

Author Comment

by:stanleyhuen
Comment Utility
i often use
$sth->fetchrow_hashref;

i don't know there is $sth->fetch;
what does it mean?

also i seldom use
$sth->bind_columns;


though i have read some doc, but my english is not so good enough to understand well.

would you please explain the above terms in simple english of their usage?

many thanks.

stanley
0
 
LVL 1

Expert Comment

by:olthoff
Comment Utility
$db = DBI->connect(db:MYSQL, name, password);

# This line connects you to the database


$db->prepare("select count(1) from table");

# This line prepares the SQL statement

$db->bind_columns(\$Count);

# This line will bind any variables that you would like to assign the search results from the SQL

$db->execute;

# This will execute the prepared SQL statement

$db->fetch;

#This will fetch the variable data and assign it to the variables you boud to the columns

$db->disconnect;

# This disconnects you from that database.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now