last day (15 days later) » 

22:05
1
A: In MYSQL/PHP how would I delete from ID 1 and insert data to ID 5

fedorquiInsert new data: INSERT INTO table (height) VALUES (xxx); Delete the pair (1,2.0): DELETE FROM table WHERE id=1; OR... all together like this: UPDATE table SET height=xxx WHERE id=1;

Yes, looks nice like this.
Tom
Tom
Now how do I reset the ID increments?
ALTER TABLE table AUTO_INCREMENT = 1;
Tom
Tom
I think I'm confused or thinking about this in the wrong way. I basically just need to insert in new data but only hold up to 5 entries. like the picture I had. Every time you insert in new data the last one will be deleted, and so on. I just thought you'd want to not increment the ID's every time you did. I'm so new to this...
Let me see if I get it: every the time you insert new data, which one will be deleted, the one with id=1 or id=5?
Tom
Tom
22:05
id = 1. So I'd always keep a history of 4 after the newly inserted data. And I'm not even sure what ID's are... or why I'd want to increment them or reset them... so this is who you're dealing with =D
And do you want to keep the ID=1 for the new item inserted or it will get ID=6?
Tom
Tom
Is it bad for the ID to increment really far? If so, I would think it would be wise to just keep it to ID 1 through 5. New data is inserted at 5. ID 1 data is delete, and everything shifts down ID's.
This is what I have so far:

`
$rand = rand(1,10);

mysql_query("INSERT INTO wave_data (height) VALUES ($rand)");

mysql_query("DELETE FROM wave_data ORDER BY id ASC LIMIT 1");
`
Well it is important in order to select the row to be emptied.

If the id is about to be kept, you can do: ``UPDATE table SET height=xxx WHERE id=1``.
Sure! But you should change the order: first delete the first, then insert the new. This way you won't exceed the 5 rows anytime.
Tom
Tom
now how would I select the 5th row, from height?
UPDATE table SET height=xxx WHERE id=(SELECT id FROM table ORDER BY id DESC LIMIT 1). The Select... gets the last one, ie, the one with greatest ID number.
Tom
Tom
22:06
so I got that, but since I'm reading it by the ID, how would I display the height from that?
Hi, Tom
let's rewind a little bit. What do you exactly want? Do you all the time have to keep less than 5 records in the table?
Tom
Tom
I want to display the 5th row's height
but weren't you asking to change it?

If you want to display the 5th, you need `SELECT height FROM table ORDER BY id DESC LIMIT 1`
Tom
Tom
To give you the context of what I'm doing. I'm scraping wave height from a buoy in the ocean every hour from a cron job that I just wrote. Where only the latest 5 readings are kept. Now I'd like to read the latest height that was inserted in.
=)
but since the ID are incrementing, I don't know how to read the 5th row and display it. Does that make sense?
by the way, SELECT height FROM table ORDER BY id DESC LIMIT 1 came up as: "Resource id #3"
This query will return the height from the row with the biggest id.
The ID is not important, you just have to know that if the AUTOINCREMENT is set, new rows will have bigger ID. So this way you will always get the last one by this SELECT ... ORDER BY id DESC LIMIT 1
Tom
Tom
22:19
ah makes sense
so why does it come out as "Resource id #3"?
You have to parse the output
mysqli_query and stuff like that
Tom
Tom
ah new stuff gotcha I can probably google it
Take a look at something like this: stackoverflow.com/questions/7462509/…
Tom
Tom
I'm literally just learning all of this today... I can't tell you how much I appreciate all of the help
I want to give you lots of stackoverflow karma
thank you so much
Haha no problem man, important thing is that you are learning
Just mark the answer as "accepted" if you feel like
Tom
Tom
22:23
oh yeah, you got it
nice : ) Good luck and keep asking if you need help
Tom
Tom
=D

last day (15 days later) »