# PHP - SQL question: delete last row from recordset

**URL:** https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025
**Category:** Uncategorized
**Created:** [July 4, 2004, 7:51pm UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025 "2004-07-04T19:51:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Flashmatazz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/flashmatazz/32/140_2.png) [@Flashmatazz](https://forum.kirupa.com/u/Flashmatazz)
#### Post date: [July 4, 2004, 7:51pm UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/1 "2004-07-04T19:51:45Z")

</div>

Here’s what I’m trying to achieve, hope somebody can help me out:

- first make a selection out of a mySQL database
- if the number of selected rows is 10 -\> delete the last row
- then insert a new row

my PHP looks like this:

```php

$query = "SELECT * FROM db_rss_uri WHERE rss_category='$category' ORDER BY rss_rate DESC, rss_id ASC LIMIT 10";
$result = mysql_query($query);
$num = mysql_numrows($result);

if ($num == 10) {
  // delete last row
}

```

What would be the best way to do this? Is there a simple way to target the last row of my selection?

I hope I’m being clear. Thanks in advance. 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 4, 2004, 8:32pm UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/2 "2004-07-04T20:32:04Z")

</div>

Do LIMIT 10,1.

That way you get the 10th row. If there isn’t a 10th row, the query won’t return any data.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 4, 2004, 9:03pm UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/3 "2004-07-04T21:03:15Z")

</div>

maybe something like

```php
if ( mysql_numrows($result) > 10 )
$query = "UPDATE table SET column = $value.. WHERE id = 10";
else 
$query = "INSERT INTO table... etc";

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 5, 2004, 7:55am UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/4 "2004-07-05T07:55:38Z")

</div>

> [@Hans Kilian](#):
>
> Do LIMIT 10,1.
> 
> That way you get the 10th row. If there isn’t a 10th row, the query won’t return any data.

Thanks, I used your suggestion.

Strange thing though is that when I use LIMIT 10,1 like this:

```php

$query = "SELECT * FROM db_rss_uri WHERE rss_category='$category' ORDER BY rss_rate DESC, rss_id DESC LIMIT 10,1";
$result = mysql_query($query) or die('Could not query');
if ($result){
  // delete last row
}
// then insert new row

```

then when the table contains exactly 10 rows, the _if_ statement is still false.  
So it looks like LIMIT 10,1 returns the 11th row?  
Anyway, when I use LIMIT 9,1 everything works like I want it to, so I’m happy 😃

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 5, 2004, 9:30am UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/5 "2004-07-05T09:30:21Z")

</div>

but why male models?

oh no wait meant, why is it 10,1 (or 9,1). I always wrote LIMIT 10 and that worked fine…

edit:you know what I’m an idiot. Just re-read the mysql statement and realized it’s getting two thins… forget it…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 5, 2004, 3:29pm UTC](https://forum.kirupa.com/t/php-sql-question-delete-last-row-from-recordset/115025/6 "2004-07-05T15:29:25Z")

</div>

Sorry, my mistake. It should be LIMIT 9,1. I guess the way to read it is ‘skip nine rows and then give me one after that’. That gives you the 10th row.
