473,575 Members | 3,477 Online
Bytes | Software Development & Data Engineering Community
+ 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 2471
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
5913
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 - integer AccessPriv - an string that varies constantly There could be identical CardNumber's, but they must have differentCompanyId's. A data set...
2
2398
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 an DataRow array containing all children rows of TableA in TableB. Then I'm sending the DataRow array as parameter into a DataAdapter.Update...
2
3778
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(); ProfileBLOBSelect.CommandText = "."; ProfileBLOBSelect.CommandType = System.Data.CommandType.StoredProcedure;
8
7850
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
997
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 but I would like to have a result-set interface that allows editing of the rows as you can when you Right-click a table in Sql Server and select Open...
2
2724
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 are 20 possible items to be ordered. i need a query that will return a unique row for every item ordered. if the same customer (record) orders 3...
3
4079
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 2-----A--------2007-09-28------10:30:00------38.75 3---MSFT----2007-09-28------09:30:00-----44.54 4 --MSFT----2007-09-28------10:00:00-----45.63 ...
4
3906
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
4381
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 table which has this date greater than today but still then i get only one record displayed. I dont find any problem anywhere but still then i dont get...
5
25664
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 | object2 | +--------------+----------+---------+ | Mr Brown | 1 | 1 |
0
7845
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7868
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8143
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6515
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5338
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3778
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1382
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.