473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error displaying records from MySQL database

3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Connect to the database
  4. $user="USERNAME"; 
  5. $password="PASSWORD";
  6. $database="DATABASE";
  7. $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error());
  8.  
  9.  
  10.  
  11. mysql_select_db($database, $con) or die( "Unable to select database");
  12. // find out how many rows are in the table 
  13. $sql = "SELECT * FROM vids";
  14. $result = mysql_query($sql, $con);
  15. $r = mysql_fetch_row($result);
  16. $numrows = $r[0];
  17.  
  18. // number of rows to show per page
  19. $rowsperpage = 1;
  20.  
  21. // find out total pages
  22. $totalpages = ceil($numrows / $rowsperpage);
  23.  
  24. // get the current page or set a default
  25. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  26.    // cast var as int
  27.    $currentpage = (int) $_GET['currentpage'];
  28. } else {
  29.    $currentpage = 1;
  30. }
  31.  
  32. // if current page is greater than total pages...
  33. if ($currentpage > $totalpages) {
  34.    // set current page to last page
  35.    $currentpage = $totalpages;
  36. } // end if
  37. // if current page is less than first page...
  38. if ($currentpage < 1) {
  39.    // set current page to first page
  40.    $currentpage = 1;
  41.  
  42. // the offset of the list, based on current page 
  43. $offset = ($currentpage - 1) * $rowsperpage;
  44.  
  45. // get the info from the db 
  46. $sql = "SELECT * FROM vids LIMIT $offset, $rowsperpage";
  47. $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
  48.  
  49. // Assign variables for videos
  50. $query  = "SELECT *  FROM vids";
  51. $result = mysql_query($query) or die ('Error: '.mysql_error ());
  52. $row = mysql_fetch_row($result);
  53.  
  54. $id = $row[0];
  55. $url = $row[1];
  56. $page_url = $row[2];
  57. $title = $row[3];
  58. $desc = $row[4];
  59. $date_add = $row[5];
  60. $date_rec = $row[6];
  61. $place = $row[7];
  62. $altitude = $row[8];
  63. $jump_no = $row[9];
  64.  
  65. // while there are rows to be fetched...
  66. while ($list = mysql_fetch_assoc($result)) {
  67.    // echo data
  68.     $id = $row[0];
  69.     $url = $row[1];
  70.     $page_url = $row[2];
  71.     $title = $row[3];
  72.     $desc = $row[4];
  73.     $date_add = $row[5];
  74.     $date_rec = $row[6];
  75.     $place = $row[7];
  76.     $altitude = $row[8];
  77.     $jump_no = $row[9];
  78.          echo "<div class='sky_cont'>
  79.     <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>
  80.     <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p>
  81.     </div>";
  82.  
  83. }
  84.  
  85.  
  86.  
  87. /******  build the pagination links ******/
  88. // if not on page 1, don't show back links
  89. if ($currentpage > 1) {
  90.    // show << link to go back to page 1
  91.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
  92.    // get previous page num
  93.    $prevpage = $currentpage - 1;
  94.    // show < link to go back to 1 page
  95.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
  96.  
  97. // range of num links to show
  98. $range = 3;
  99.  
  100. // loop to show links to range of pages around current page
  101. for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
  102.    // if it's a valid page number...
  103.    if (($x > 0) && ($x <= $totalpages)) {
  104.       // if we're on current page...
  105.       if ($x == $currentpage) {
  106.          // 'highlight' it but don't make a link
  107.          echo " [<b>$x</b>] ";
  108.       // if not current page...
  109.       } else {
  110.          // make it a link
  111.          echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
  112.       }
  113.    } 
  114. }
  115.  
  116. // if not on last page, show forward and last page links        
  117. if ($currentpage != $totalpages) {
  118.    // get next page
  119.    $nextpage = $currentpage + 1;
  120.     // echo forward link for next page 
  121.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
  122.    // echo forward link for lastpage
  123.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
  124. } // end if
  125. /****** end build pagination links ******/
  126.  
  127.  
  128. ?>
  129.  
  130.  
  131.  
Aug 19 '10 #1
4 1869
Dormilich
8,658 Recognized Expert Moderator Expert
it is good style to name all the fields in an SQL query explicitly, because that can save you much memory, esp. when you don’t need all fields. additionally, you can see by looking at the query, what it should contain (doesn’t require knowledge of the DB itself).

Expand|Select|Wrap|Line Numbers
  1. // inefficient
  2. SELECT * FROM table
  3. // better
  4. SELECT `field_1`, `field_2`, `field_3` FROM table
one more point, you can pass the fetch mode in the mysql_fetch_* function, so that PHP doesn’t create more array members than necessary (currently you have a named and numerically indexed array).

Expand|Select|Wrap|Line Numbers
  1. // get the info from the db 
  2. $sql = "SELECT * FROM vids LIMIT $offset, $rowsperpage";
  3. $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
  4.  
  5. // Assign variables for videos
  6. $query  = "SELECT *  FROM vids";
  7. $result = mysql_query($query) or die ('Error: '.mysql_error ());
you’re overwriting the first query immediately …

Expand|Select|Wrap|Line Numbers
  1. while ($list = mysql_fetch_assoc($result)) {
  2.     $id = $row[0];
do you see something obvious?
Aug 20 '10 #2
Gary Baardman
3 New Member
I can see what the problem is with the overwritting of the variable $results, simply rename the second variable?

I dont understand what is wrong with the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. while ($list = mysql_fetch_assoc($result)) {
  3.     $id = $row[0];
  4.  
  5.  
I have modified the script a little, but it still does not display the correct information - only the first record.

PHP code is below:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Connect to the database
  4. $user="USERNAME"; 
  5. $password="PASSWORD";
  6. $database="DATABASE";
  7. $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error());
  8.  
  9. mysql_select_db($database, $con) or die( "Unable to select database");
  10.  
  11. // find out how many rows are in the table 
  12. $sql = "SELECT * FROM vids";
  13. $result = mysql_query($sql, $con);
  14. $r = mysql_fetch_row($result);
  15. $numrows = $r[0];
  16.  
  17. // number of rows to show per page
  18. $rowsperpage = 3;
  19.  
  20. // find out total pages
  21. $totalpages = ceil($numrows / $rowsperpage);
  22.  
  23. // get the current page or set a default
  24. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  25.    // cast var as int
  26.    $currentpage = (int) $_GET['currentpage'];
  27. } else {
  28.    $currentpage = 1;
  29. }
  30.  
  31. // if current page is greater than total pages...
  32. if ($currentpage > $totalpages) {
  33.    // set current page to last page
  34.    $currentpage = $totalpages;
  35. } // end if
  36. // if current page is less than first page...
  37. if ($currentpage < 1) {
  38.    // set current page to first page
  39.    $currentpage = 1;
  40.  
  41. // the offset of the list, based on current page 
  42. $offset = ($currentpage - 1) * $rowsperpage;
  43.  
  44.  
  45.  
  46. // Assign variables for videos
  47. /*$query  = "SELECT *  FROM vids";
  48. $result = mysql_query($query) or die ('Error: '.mysql_error ());
  49. $row = mysql_fetch_row($result);
  50.  
  51. $id = $row[0];
  52. $url = $row[1];
  53. $page_url = $row[2];
  54. $title = $row[3];
  55. $desc = $row[4];
  56. $date_add = $row[5];
  57. $date_rec = $row[6];
  58. $place = $row[7];
  59. $altitude = $row[8];
  60. $jump_no = $row[9];*/
  61.  
  62. // get the info from the db 
  63. $sql = "SELECT * FROM vids LIMIT $offset, $rowsperpage";
  64. $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
  65. $row = mysql_fetch_row($result);
  66.  
  67. $id = $row[0];
  68. $url = $row[1];
  69. $page_url = $row[2];
  70. $title = $row[3];
  71. $desc = $row[4];
  72. $date_add = $row[5];
  73. $date_rec = $row[6];
  74. $place = $row[7];
  75. $altitude = $row[8];
  76. $jump_no = $row[9];
  77.  
  78.  
  79. // while there are rows to be fetched...
  80. while ($list = mysql_fetch_assoc($result)) {
  81.  
  82.     $id = $row[0];
  83. $url = $row[1];
  84. $page_url = $row[2];
  85. $title = $row[3];
  86. $desc = $row[4];
  87. $date_add = $row[5];
  88. $date_rec = $row[6];
  89. $place = $row[7];
  90. $altitude = $row[8];
  91. $jump_no = $row[9];
  92.  
  93.          echo "<div class='sky_cont'>
  94.     <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>
  95.     <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p>
  96.     </div>";
  97.  
  98. }
  99.  
  100.  
  101.  
  102. /******  build the pagination links ******/
  103. // if not on page 1, don't show back links
  104. if ($currentpage > 1) {
  105.    // show << link to go back to page 1
  106.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
  107.    // get previous page num
  108.    $prevpage = $currentpage - 1;
  109.    // show < link to go back to 1 page
  110.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
  111.  
  112. // range of num links to show
  113. $range = 3;
  114.  
  115. // loop to show links to range of pages around current page
  116. for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
  117.    // if it's a valid page number...
  118.    if (($x > 0) && ($x <= $totalpages)) {
  119.       // if we're on current page...
  120.       if ($x == $currentpage) {
  121.          // 'highlight' it but don't make a link
  122.          echo " [<b>$x</b>] ";
  123.       // if not current page...
  124.       } else {
  125.          // make it a link
  126.          echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
  127.       }
  128.    } 
  129. }
  130.  
  131. // if not on last page, show forward and last page links        
  132. if ($currentpage != $totalpages) {
  133.    // get next page
  134.    $nextpage = $currentpage + 1;
  135.     // echo forward link for next page 
  136.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
  137.    // echo forward link for lastpage
  138.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
  139. } // end if
  140. /****** end build pagination links ******/
  141.  
  142.  
  143. ?>
  144.  
  145.  
  146.  
Thanks,
Gary
Aug 20 '10 #3
JKing
1,206 Recognized Expert Top Contributor
The problem is that you create your loop with the variable $list and it loops three times but you are echo'ing the variables stored in $row not $list. The variables in $row were set just above your loop to the first record in the results which would explain why you get the same result duplicated.

Expand|Select|Wrap|Line Numbers
  1. // get the info from the db 
  2. $sql = "SELECT * FROM vids LIMIT $offset, $rowsperpage";
  3. $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
  4.  
  5. // while there are rows to be fetched...
  6. while ($row = mysql_fetch_assoc($result)) {
  7.  
  8. $id = $row[0];
  9. $url = $row[1];
  10. $page_url = $row[2];
  11. $title = $row[3];
  12. $desc = $row[4];
  13. $date_add = $row[5];
  14. $date_rec = $row[6];
  15. $place = $row[7];
  16. $altitude = $row[8];
  17. $jump_no = $row[9];
  18.  
  19.          echo "<div class='sky_cont'>
  20.     <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>
  21.     <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p>
  22.     </div>";
  23.  
  24. }
Try that.
Aug 25 '10 #4
Gary Baardman
3 New Member
Thanks JKing, your code got me thinking and I managed to tweak it so it works now.

The full working code is below:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Connect to the database
  4. $user="USER"; 
  5. $password="PASSWORD";
  6. $database="DATABASE";
  7. $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error());
  8. mysql_select_db($database, $con) or die( "Unable to select database");
  9.  
  10.  
  11. // Determine number of rows in database
  12. $query = mysql_query("SELECT * FROM vids");
  13. $numrows=mysql_num_rows($query);
  14.  
  15. // number of rows to show per page
  16. $rowsperpage = 2;
  17.  
  18. // find out total pages
  19. $totalpages = ceil($numrows / $rowsperpage);
  20.  
  21. // get the current page or set a default
  22. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  23.    // cast var as int
  24.    $currentpage = (int) $_GET['currentpage'];
  25. } else {
  26.    $currentpage = 1;
  27. }
  28.  
  29. // if current page is greater than total pages...
  30. if ($currentpage > $totalpages) {
  31.    // set current page to last page
  32.    $currentpage = $totalpages;
  33. } // end if
  34. // if current page is less than first page...
  35. if ($currentpage < 1) {
  36.    // set current page to first page
  37.    $currentpage = 1;
  38.  
  39. // the offset of the list, based on current page 
  40. $offset = ($currentpage - 1) * $rowsperpage;
  41.  
  42. // get the info from the db 
  43. $result = mysql_query("SELECT * FROM vids");
  44. while($row = mysql_fetch_array($result))
  45.   {
  46.  
  47. $id = $row['id'];  
  48. $url = $row['url'];
  49. $page_url = $row['page_url'];;
  50. $title = $row['title'];
  51. $desc = $row['desc'];
  52. $date_add = $row['date_add'];
  53. $date_rec = $row['date_rec'];
  54. $place = $row['place'];
  55. $altitude = $row['altitude'];
  56. $jump_no = $row['jump_no'];
  57.  
  58. //Display video
  59. echo "<div class='sky_cont'> 
  60.     <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>
  61.     <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p>
  62.     </div>";
  63. }
  64.  
  65.  
  66. /******  build the pagination links ******/
  67. // if not on page 1, don't show back links
  68. if ($currentpage > 1) {
  69.    // show << link to go back to page 1
  70.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
  71.    // get previous page num
  72.    $prevpage = $currentpage - 1;
  73.    // show < link to go back to 1 page
  74.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
  75.  
  76. // range of num links to show
  77. $range = 3;
  78.  
  79. // loop to show links to range of pages around current page
  80. for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
  81.    // if it's a valid page number...
  82.    if (($x > 0) && ($x <= $totalpages)) {
  83.       // if we're on current page...
  84.       if ($x == $currentpage) {
  85.          // 'highlight' it but don't make a link
  86.          echo " [<b>$x</b>] ";
  87.       // if not current page...
  88.       } else {
  89.          // make it a link
  90.          echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
  91.       }
  92.    } 
  93. }
  94.  
  95. // if not on last page, show forward and last page links        
  96. if ($currentpage != $totalpages) {
  97.    // get next page
  98.    $nextpage = $currentpage + 1;
  99.     // echo forward link for next page 
  100.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
  101.    // echo forward link for lastpage
  102.    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
  103. } // end if
  104. /****** end build pagination links ******/
  105.  
  106.  
  107. ?>
  108.  
The pagination works, but does not limit the number of records shown. I have set the number of rows (records) to be shown per page as 2. I have 4 records in my database and the pagination should show 2 records per page, with a total of two pages. The problem is, it displays all 4 records on pages 1 and two.

How can I restrict the number of records shown on each page? I thought my code would have done that, but as it seems, it shows all records on the calculated number of pages.

Thanks for your help :)
Aug 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1769
by: hansyin | last post by:
HI, I got a problem about restoring data in mysql: I have 2 or more PCs installed with mysql database, with same or almost same table structure, but with different data. Can I combine those data...
2
1651
by: donpro | last post by:
Hi, I have a varchar field in a MySQL database that contains a line of text like so: "This is a line if text" The double quotes are included in the database field. I cannot seem to...
0
5732
by: Hans | last post by:
Hi, That's my first time to send mail to this address for asking help. Sorry for my poor english firstly. My case is like this: Many guys are using a mysql database, each guy has a database...
0
1094
by: madan26 | last post by:
Dear Friends I am getting a problems. I have data display page. If the records remains below 5000 it works very fine. but if records goes beyond 5000 it gives a problem as given below:- ...
11
5211
by: menmysql | last post by:
i am getting the following error while connecting mysql database using jsp java.sql.SQLException: Communication link failure: Bad handshake can any one tell what is the actual problem
8
4625
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and...
8
7541
by: bimeldip | last post by:
Hi, i would like to display the list of tables in a database in a drop down list then when user selects a table, the table will be dispalyed on the page. So far i've done this: <? $dbname =...
6
2157
by: fpcreator2000 | last post by:
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database. It uploads two files into two distinct folder, and it takes the filenames and inserts...
0
1452
by: John Kirkpatrick | last post by:
Hi all, I am having difficulty displaying records on a frontend MS Access 2000 form using a MySQL backend. The following code works well with the Jet database engine but doesn't work properly...
221
366957
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.