• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

divide into pages

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
stanleyhuen
Asked:
stanleyhuen
  • 4
  • 3
1 Solution
 
olthoffCommented:
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
 
stanleyhuenAuthor Commented:
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
 
stanleyhuenAuthor Commented:
what is count(1) mean?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
olthoffCommented:
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
 
stanleyhuenAuthor Commented:
Adjusted points from 13 to 30
0
 
stanleyhuenAuthor Commented:
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
 
olthoffCommented:
$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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now