473,490 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to return the last 10 rows of a database

16 New Member
Hello, I'm trying to get A recent searches to echo onto my website, but i don't know how I need to modify my query to get this to work.
here iswhat I have so far:
Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.  
  3.  
  4.  
  5. //connect to your database 
  6. mysql_connect("localhost",$user,$pass); //(host, username, password)
  7.  
  8. //specify database 
  9. mysql_select_db($db) or die("Unable to select database"); //select which database we're using
  10.  
  11.  
  12.  
  13. $result = mysql_query("SELECT * FROM RecentSearches ");
  14.  
  15.  
  16.  
  17.  
  18. // begin to show results set
  19. echo "<b><u>Results</b></u> <br/><br/><br/>";
  20.  
  21.  
  22. // now you can display the results returned
  23.   while ($row= mysql_fetch_array($result)) {
  24.   $title = $row["song_name"];
  25.   $artist = $row["artist"];
  26.   $Buy = $row["purchase_link"];
  27.  
  28.  
  29.  
  30.   echo "$title - " ;
  31.   echo  "$artist - "  ;
  32.   printf('<a href="%s" target="_tab">Buy this track</a>', $Buy);
  33.   echo "<br/><br/>";
  34.  
  35.   }
  36.  
  37.  
  38. ?>
  39.  
This just throws every song that's been searched onto the page in a massive list, but I only want the last 10 searched (so the last 10 entries in the table). Any ideas?
May 5 '11 #1
4 2465
JKing
1,206 Recognized Expert Top Contributor
What other fields are in your table?

To retrieve ten results you can use the Limit keyword in your sql, and to get the latest 10 you will want to use ORDER BY ideally on a date field.

It would look something like this:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM RecentSearches ORDER BY date_added DESC LIMIT 10";
  2. $results = mysql_query($sql);
  3.  
Replace date_added with your date field if you have one if not the ID should also work.
May 5 '11 #2
Dan Gaffey
16 New Member
I have the songID so I could just order descending using that because the highest songID is the most recent one, correct?
May 5 '11 #3
Dan Gaffey
16 New Member
Oh, just read about the ID field, that's what I get for skim reading on the bus. I'll try it out when I'm at a computer. Thankyou :)
May 5 '11 #4
Dan Gaffey
16 New Member
Works perfectly! Have a list of songs that refreshes the ten most recent now, thanks :)
May 6 '11 #5

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

Similar topics

4
5910
by: Ted | last post by:
Here's what I'm trying to do: I have a table, which I don't have administration over, with 4 interestingfields: RecordCount - an auto incrementing primary key CardNumber - integer CompanyID -...
2
2382
by: raffe | last post by:
Hi all. I'm struggeling with a problem. I trying to do something very simple. I have a DataSet containing two tables TableA and TableB. I use TableA.GetChildren("relationName") in order to get...
2
3769
by: Hrvoje Voda | last post by:
I'm using this store procedure to return data from sql server. SqlDataReader sqlRead = null; System.Data.SqlClient.SqlCommand ProfileBLOBSelect = new System.Data.SqlClient.SqlCommand(); ...
8
7847
by: kingskippus | last post by:
I don't know if this is possible, but I haven't been able to find any information. I have two tables, for example: Table 1 (two columns, id and foo) id foo --- ----- 1 foo_a 2 foo_b
0
981
by: Elliot | last post by:
I am using the following code to launch Sql Server and execute a query: Process.Start("c:\query.sql") where query.sql contains "Select * from Table1" The result is a read only result-set...
2
2697
by: jennk | last post by:
i am working in Access 97, our database tables are linked from ODBCsqlsvr (not even sure what that means). i have a table where each record has a unique customer and their order information. there...
3
4073
by: daniyalnawaz | last post by:
Hi! Suppose there is a table MarketData and has following data id--symbol--------date--------------time------price 1-----A--------2007-09-28------09:30:00------37.03 ...
4
3898
by: zergziad | last post by:
Hi All, I need help on how to return the rows in one particular field as a string with comma separated in between those data. For example: Table A ID Name IsActive...
4
4376
by: raaman rai | last post by:
Pls see the code below, where i wanted to select all the records from my table where the submdate (submission date) is greater or equal to today's date. Whereas i have two or three records in my...
5
25638
bilibytes
by: bilibytes | last post by:
Hi, i am having big headaches in trying to find out how to perform a very very simple query. having a table like this one: +--------------+----------+---------+ | name | object1 |...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
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
7356
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...
1
4878
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
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.