I am having a few problems returning records from a mySQL database using PHP.
Firstly I must say I am trying to create a video album using MySQL, PHP and using pagination.
My first problem is if I want to retrieve the first 3 records, only the first record is retrieved and then the next two records are duplicates of the first record.
My second problem is if I want to retrieve all of the records (there are currently 3), only the total-1 records are returned - I can not retrieve the last record.
Below is my PHP code for the script:
Firstly I must say I am trying to create a video album using MySQL, PHP and using pagination.
My first problem is if I want to retrieve the first 3 records, only the first record is retrieved and then the next two records are duplicates of the first record.
My second problem is if I want to retrieve all of the records (there are currently 3), only the total-1 records are returned - I can not retrieve the last record.
Below is my PHP code for the script:
Code:
<?php //Connect to the database $user="USERNAME"; $password="PASSWORD"; $database="DATABASE"; $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error()); mysql_select_db($database, $con) or die( "Unable to select database"); // find out how many rows are in the table $sql = "SELECT * FROM vids"; $result = mysql_query($sql, $con); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 1; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT * FROM vids LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR); // Assign variables for videos $query = "SELECT * FROM vids"; $result = mysql_query($query) or die ('Error: '.mysql_error ()); $row = mysql_fetch_row($result); $id = $row[0]; $url = $row[1]; $page_url = $row[2]; $title = $row[3]; $desc = $row[4]; $date_add = $row[5]; $date_rec = $row[6]; $place = $row[7]; $altitude = $row[8]; $jump_no = $row[9]; // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data $id = $row[0]; $url = $row[1]; $page_url = $row[2]; $title = $row[3]; $desc = $row[4]; $date_add = $row[5]; $date_rec = $row[6]; $place = $row[7]; $altitude = $row[8]; $jump_no = $row[9]; echo "<div class='sky_cont'> <div class='sky_vid'><a class='video' href=\"$url\"><img src='http://www.netlinksurveyors.co.uk/test/images/lgo.jpg' alt=\"$title\" Border='0' /></a></div> <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p> </div>"; } /****** build the pagination links ******/ // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // range of num links to show $range = 3; // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?>
Comment