Hey there.. It has been a long time since I wrote a tutorial.. but now I thought I'll do it.. So let's start
First we have to connect to our database:
Now we are connected to database.. Let's get our information.Code:// Connect to your database
$mysql_con = mysql_connect("localhost", "Username", "Password");
// Let's check if there were any errors
if(!$mysql_con){
// If there was ań error, if there was, lets display an error
die(mysql_error());
}
// Select your database
$mysql_db = mysql_select_db("MyDatabase");
// If there were any errors with selecting your database, display an error
if(!$mysql_db){
die(mysql_error());
}
First we need to get our page number:
Now we need to get all messages from database and count them:Code:// If the page wasn't set, lets set $page to number 1 for the first page
if($page == ""){
$page = "1";
}else{
// If page is set, lets get it
$page = $_GET['page'];
}
When these things are done.. lets set our numbers:Code:// Now lets get all messages from your database
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);
// Lets count all messages
$num = mysql_num_rows($query);
Now it's time for "Next" , "Previous" First and last page..Code:// Lets set how many messages we want to display
$per_page = "10";
// Now we must calculate the last page
$last_page = ceil($num/$per_page);
// And set the first page
$first_page = "1";
:
Code:// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";
// If page is 1 then remove link from "Previous" word
if($page == $first_page){
echo "Previous ";
}else{
if(!isset($page)){
echo "Previous ";
}else{
// But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
$previous = $page-1;
echo "<a href='?page=".$previous."'>Previous</a> ";
}
}
// If the page is last page.. lets remove "Next" link
if($page == $last_page){
echo "Next ";
}else{
// If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
if(!isset($page)){
$next = $first_page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}else{
$next = $page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}
}
// And now lets add the "Last page" link
echo "<a href='?page=".$last_page."'>Last page</a>";
![]()
Now lets do some math and get our messages :
Voila.. it wasn't that hard right ?Code:// Math.. It gets us the start number of message that will be displayed
$start = ($page-1)*$per_page;
// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";
// It's time for getting our messages
$sql = "SELECT * FROM messages $limit";
$query = mysql_query($sql);
echo "<br /><br />";
// And lets display our messages
while($row = mysql_fetch_array($query) or die(mysql_error())){
echo $row['message']."<br />";
}
Here's the full code with comments:
I hope it helped you a littleCode:<?php
// Connect to your database
$mysql_con = mysql_connect("localhost", "Username", "Password");
// Let's check if there were any errors
if(!$mysql_con){
// If there was ań error, if there was, let's display an error
die(mysql_error());
}
// Select your database
$mysql_db = mysql_select_db("MyDatabase");
// If there were any errors with selecting your database, display an error
if(!$mysql_db){
die(mysql_error());
}
// If the page wasn't set, lets set $page to number 1 for the first page
if($page == ""){
$page = "1";
}else{
// If page is set, let's get it
$page = $_GET['page'];
}
// Now lets get all messages from your database
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);
// Lets count all messages
$num = mysql_num_rows($query);
// Lets set how many messages we want to display
$per_page = "10";
// Now we must calculate the last page
$last_page = ceil($num/$per_page);
// And set the first page
$first_page = "1";
// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";
// If page is 1 then remove link from "Previous" word
if($page == $first_page){
echo "Previous ";
}else{
if(!isset($page)){
echo "Previous ";
}else{
// But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
$previous = $page-1;
echo "<a href='?page=".$previous."'>Previous</a> ";
}
}
// If the page is last page.. lets remove "Next" link
if($page == $last_page){
echo "Next ";
}else{
// If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
if(!isset($page)){
$next = $first_page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}else{
$next = $page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}
}
// And now lets add the "Last page" link
echo "<a href='?page=".$last_page."'>Last page</a>";
// Math.. It gets us the start number of message that will be displayed
$start = ($page-1)*$per_page;
// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";
// It's time for getting our messages
$sql = "SELECT * FROM messages $limit";
$query = mysql_query($sql);
echo "<br /><br />";
// And lets display our messages
while($row = mysql_fetch_array($query) or die(mysql_error())){
echo $row['message']."<br />";
}
?>
Regards,
Jaan
Last edited by Alexander; 04-18-2011 at 10:19 PM. Reason: Restoration
Thank you![]()
Very nice. +rep
Very handy! +rep
Epic failure. (just kidding, but in all seriousness)Jordan did this and crippled the CodeCall server.Code:// Now lets get all messages from your database
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);
// Lets count all messages
$num = mysql_num_rows($query);
is the preferred method of counting the rows in a database.Code:$num = mysql_query("SELECT COUNT(*) FROM messages");
nice tutorial +rep
@john: is theres a performance difference between these 2:
-"select count(`some_field_name`) from table"
-"select count(*) from table"
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Yes assuming the MyISAM engine is being used (and not Innodb - MyISAM is default) COUNT(*) will have the best performance. MyISAM caches the table row number so COUNT(*) is instantaneous. COUNT(`col`) could cause MySQL to perform a table scan depending on the column attributes.
I believe, if we look hard enough, we can find a flaw in everyone's tutorial. I looked over some of your tutorials from the past they also have errors.
Check this out: COUNT(*) vs COUNT(col) | MySQL Performance Blog
Good performance information there.
Well.. What can I say..
both ways work.. so..
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks