473,464 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

delete last row from specific user

155 New Member
i got for example results like that

user1
user1
user2
user1
user1
user3
user1

i want to limit user1 to have maximum 4 resilts ,if user1 have 5 results it will be deleted ,this is example of code that im trying to build:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $query = mysql_query("SELECT * FROM `my_table` where username='$username' ") or die(mysql_error());
  3. $last = '';
  4. $count = 0;
  5. while($line = mysql_fetch_assoc($query)) {
  6.   if($last == $line['username']) $count++; 
  7.   else {
  8.     $last = $line['username'];
  9.     $count = 1;
  10.   }
  11.   if($count > 4) {
  12. $sql_delete="DELETE FROM my_table WHERE username = '$username'";
  13. mysql_query($sql_delete)or die(mysql_error());
  14.   }
  15. ?>
what i need is to delete the oldest record from the specific user ,but it is not doing that!! ,please help!

thanx in advanced.
May 12 '09 #1
7 2000
Ciary
247 Recognized Expert New Member
i can guess what does happen: all records from user1 are deleted when he has more then 4 records.

the problem is very simple. if you use a delete query it will delete every record that matches with the given patern.
Expand|Select|Wrap|Line Numbers
  1. WHERE username = '$username'"
what you need to do is this. in your while-loop you save the ID or the primary key of your record in a variable.

then you use the same query but this time you use
Expand|Select|Wrap|Line Numbers
  1. WHERE PK_ID= '$lastID'"
May 12 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
there's probably a way to do that using SQL too (using subqueries and such), but you should ask the SQL guys, because that's a more advanced matter.
May 12 '09 #3
canabatz
155 New Member
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $query = mysql_query("SELECT * FROM `my_table` where username='$username' order by id asc") or die(mysql_error());
  3. $last = '';
  4. $count = 0;
  5. while($line = mysql_fetch_assoc($query)) {
  6.   if($last == $line['username']) $count++; 
  7.   else {
  8.     $last = $line['username'];
  9.     $count = 1;
  10.   }
  11.   if($count > 4) {
  12. $sql_delete="DELETE FROM my_table WHERE username = '$username' order by id asc limit 1";
  13. mysql_query($sql_delete)or die(mysql_error());
  14.   }
  15. ?>
i got the last id from user1 ,and after i delete the oldest id from user1 with limit 1

is it going to work like that? i didnt try it!

thanx
May 12 '09 #4
Ciary
247 Recognized Expert New Member
i'm not sure. never used delete with limit. i think you just need to try if it does what you need. if it does, pls report back at the forum. i think this might also be helpful to many others :)
May 12 '09 #5
canabatz
155 New Member
working perfect!! :)
May 12 '09 #6
canabatz
155 New Member
thanx all for your help!
May 12 '09 #7
Ciary
247 Recognized Expert New Member
np, i'm glad to help :)
May 12 '09 #8

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

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
1
by: Someonekicked | last post by:
I have a binary file, and I need to delete a specific number of characters in it. The file contains records, each record has a specific length. So the way I wanna handle deleting is that I will...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
0
by: teddymeu | last post by:
Hi Guys, since I've done nothing but ask questions these last few weeks to get my first application up and running I thought it was about time to share the wealth and help out a newbie like me since...
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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
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
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,...
0
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
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...

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.